【发布时间】: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中定义x、y、getX或getY- 您可以继承这些属性。
标签: java constructor return super