0

我正在尝试使用 Java 博士制作一个方形类。我已经从一个矩形类中获取了大部分代码,但它让我一团糟。我目前是 Java 的初学者,所以我现在真的迷路了。如果您对如何纠正我的方形课程有任何更正或提示,请告诉我。谢谢

    package graphics2;

/**
 * A class that represents a square with given origin, width, and height.
 */
public class Square extends Graphics {
  // The private width and height of this square.
  private double width = 0;
  private double height = 0;  

  /**
   * Constructs a square with given origin, width, and height.
   */
  public Square(Point origin, double side) {
    super(origin, side, side);
    setOrigin(new Point(0, 0));
    width = height = side;
}

  /**
   * Constructs a square with given width and height at origin (0, 0).
   */
  public Square(double side) {
    setOrigin(new Point(0, 0));
    width = height = side;
  }
  /**
   * Returns the square's side of this square.
   */
  public double  getSide() {return width;}

  /**
   * Returns the width coordinate of this square.
   */
  public double getWidth() {return width; }

  /**
   * Returns the height coordinate of this square.
   */
  public double getHeight() {return height; }

  /**
   * Returns the area of this square.
   */
  public double area() {
    return width * height;
  }
}

这也是我收到的错误:

    1 error found:
File: C:\Users\GreatOne\Desktop\06Labs-\graphics2\Square.java  [line: 15]
Error: The constructor graphics2.Graphics(graphics2.Point, double, double) is undefined
4

2 回答 2

5

克里斯,不要扩展图形。这是非常错误的。不要扩展任何东西。

您的构造函数需要更正,它们与您尝试创建 Squares 的方式不匹配。

此外,您还缺少多个变量。

我建议您打开教科书或在线阅读一些教程,而不是让 stackoverflow 上的人来解决这个烂摊子。我可以在几分钟内为你解决这个问题,但它不会帮助你,因为我怀疑你会理解如何使用它。

请好好学习。你会因为它变得更好。

于 2013-07-06T03:48:35.743 回答
0

您的代码根据错误解释:

错误

  • 错误 1 ​​[行:15]:构造函数未定义。你想要的是super(origin, side);,而不是super(origin, side, side);我假设。要么,要么你应该定义构造函数super(origin, side, side);

  • 错误 2,3,4,5 [line: 17, 18, 26, 27]:您在构造函数内部使用变量wh而没有将它们作为方法参数。由于这是您想要的正方形,因此在两个构造函数中都更改为width = wand 。当您在方法参数中传递变量时,这不会产生问题。height = hwidth = sideheight = sideside

  • 最后一个错误 [line: 32] : You are return the variable sidein the getter method public double getSide()。由于side不是类的变量,因此显示错误。将其更改为return widthreturn height。由于这是一个正方形,因此两者将相等。

于 2013-07-06T03:56:14.047 回答