我有一个带有 2 个变量的超类 RECTANGLE 和一个带有 1 个变量的子类 SQUARE。我正在使用类 Square 来继承 getArea() 方法并很好地覆盖它。Eclipse 编辑器在我的 SQUARE 类中出现错误,“super(width,length);”。LENGTH 变量有一个错误,可以通过在 RECTANGLE 类中将其设为静态来修复,这不是我想要的。我的作业要求 SQUARE 类有一个带有 1 个变量的构造函数,可以自行相乘。我的代码中的逻辑错误是什么?
public class Rectangle
{
double width, length;
Rectangle(double width, double length)
{
this.width = width;
this.length = length;
}
double getArea()
{
double area = width * length;
return area;
}
void show()
{
System.out.println("Rectangle's width and length are: " + width + ", " + length);
System.out.println("Rectangle's area is: " + getArea());
System.out.println();
}
}
public class Square extends Rectangle
{
double width, length;
Square(double width)
{
super(width, length);
this.width = width;
}
double getArea()
{
double area = width * width;
return area;
}
void show()
{
System.out.println("Square's width is: " + width) ;
System.out.println("Square's area is: " + getArea());
}
}
public class ShapesAPP
{
public static void main(String[] args)
{
Rectangle shape1 = new Rectangle(5, 2);
Square shape2 = new Square(5);
shape1.show( );
shape2.show( );
}
}