【问题标题】:Working with super in java在 java 中使用 super
【发布时间】:2016-08-01 19:37:56
【问题描述】:

对于Cube 类,我正在尝试摆脱错误:

Cube.java:12: error: constructor Rectangle in class Rectangle cannot be applied to given types;
    super(x, y);
    ^
  required: int,int,double,double
  found: int,int.......

我知道 Cube 的每个面都是 Rectangle,其长度和宽度需要与 Cube 的边相同,但我不确定需要将什么传递给 Rectangle 构造函数以使其长度和宽度成为与立方体的侧面相同。

还试图计算体积,即矩形的面积乘以立方体边的长度

这是 Cube 类

// ---------------------------------
// File Description:
//   Defines a Cube
// ---------------------------------

public class Cube extends Rectangle
{


  public Cube(int x, int y, int side)
  {
    super(x, y);
    side = super.area(); // not sure if this is right
  }


  public int    getSide()   {return side;}

  public double area()      {return 6 * super.area();}
  public double volume()    {return super.area() * side;}
  public String toString()  {return super.toString();}
}

这是矩形类

// ---------------------------------
// File Description:
//   Defines a Rectangle
// ---------------------------------

public class Rectangle extends Point
{
  private int    x, y;  // Coordinates of the Point
  private double length, width;

  public Rectangle(int x, int y, double l, double w)
  {
    super(x, y);
    length = l;
    width = w;
  }

  public int       getX()         {return x;}
  public int       getY()         {return y;}
  public double    getLength()    {return length;}
  public double    getWidth()     {return width;}

  public double area()      {return length * width;}
  public String toString()  {return "[" + x + ", " + y + "]" + " Length = " + length + " Width = " + width;}
}

【问题讨论】:

  • 由于Rectangle 没有带2 个参数的构造函数,您希望在调用super(x, y); 时调用什么代码?
  • 您确定您的Cube 需要从Rectangle 继承,而不是简单地保存一个Rectangles 数组,每个数组都有自己的Point 和长度?此外,由于Rectangle 扩展了Point,因此您无需在Rectangle 中定义xygetXgetY - 您可以继承这些属性。

标签: java constructor return super


【解决方案1】:

这个对象的构造似乎错过了扩展的想法。

Cube 不是RectangleCube 可以被认为是多个 Rectangles 与空间数据的组合,但矩形应该是 Cube 的成员(读取属性/字段)。

为了说明这一点,请考虑以下陈述之间的区别。

所有立方体都是矩形。

所有的猫都是动物。

您可以想象创建一个扩展超类AnimalCat 对象。 Cubes 和 Rectangles 没有这种关系。

考虑将您的代码重构为:

public class Cube {
    private List<Rectangle> faces:

    ....

}

更进一步,并非所有Rectangles 都是Points。

一个点是一对 x, y 坐标。为了准确地绘制Rectangle,所需的最少信息量是两个Points。

+--
| |
--+

如果你有对角Points(这里用+标记)你可以画你的Rectangle

鉴于此,也许您还应该重构 Rectangle 以拥有一对 Points 作为成员。

类似:

public class Rectangle {
    private Point firstCorner;
    private Point secondCorner;

    ...
}

【讨论】:

    【解决方案2】:

    super(x, y) 中的参数数量与 Rectangle 类的构造函数中的参数不匹配。当您显式调用超类构造函数时,您必须确保超类具有匹配的构造函数。

    【讨论】:

      【解决方案3】:

      Rectangle 的超类是 Cube 而不是 Point。使用super() 可以调用Cube 的构造函数而不是Point 的构造函数

      【讨论】:

        【解决方案4】:

        问题是 Rectangle 构造函数需要 4 个参数,而您在创建 Cube 的实例时只传递了 2 个参数。你也应该通过你的长度:

          public Cube(int x, int y, int side)
          {
            super(x, y, side, side);
          }
        

        如果您稍微思考一下,它就很有意义 - 任何矩形都需要原点 x 和 y,以及宽度和高度。如果立方体的宽度和高度相同,但您仍然应该告诉矩形它们的值是什么;)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-15
          • 2015-11-07
          • 2023-03-10
          • 1970-01-01
          • 2018-05-30
          相关资源
          最近更新 更多