Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何设计从矩形类继承的方形类_Java_Inheritance_Abstract Class - Fatal编程技术网

Java 如何设计从矩形类继承的方形类

Java 如何设计从矩形类继承的方形类,java,inheritance,abstract-class,Java,Inheritance,Abstract Class,因此,我正在尝试为我的一个教程编写一些代码。输入和预期输出如下: > Square s = new Square(5); > s.toString(); < Square with area 25.00 and perimeter 20.00 问题是,当我尝试编译它时,会出现以下错误: error: no suitable constructor found for Rectangle(no arguments) public Square(double side)

因此,我正在尝试为我的一个教程编写一些代码。输入和预期输出如下:

> Square s = new Square(5);
> s.toString();
< Square with area 25.00 and perimeter 20.00
问题是,当我尝试编译它时,会出现以下错误:

error: no suitable constructor found for Rectangle(no arguments)
    public Square(double side) {
                               ^
    constructor Rectangle.Rectangle(double) is not applicable
      (actual and formal argument lists differ in length)
    constructor Rectangle.Rectangle(double,double) is not applicable
      (actual and formal argument lists differ in length)
我不确定在这种情况下继承是如何工作的。如何修改代码,使输入返回正确的输出?我假定错误只存在于Square类中,因为代码编译时不是这样


提前感谢。

您必须在继承类中显式调用
super
构造函数。 如果你想考虑@runec的答案(是的,我投了更高的票,因为它很好),你可能想从
Rectangle
中删除只有一个参数的构造函数,并让
Square
处理一个4条等边的矩形

public class Rectangle extends Shape {
    protected double width;
    protected double height;

    public Rectangle(double width, double height) {
        this.shapeName = "Rectangle";
        this.width = width;
        this.height = height;
    }

    public double getArea() {
        return width * height;
    }

    public double getPerimeter() {
        return 2 * (width + height);
    }
}
这里有一个只接受一个
double
参数的
Square

public class Square extends Rectangle {

    public Square(double side) {
        super(side, side);
        this.shapeName = "Square";
        this.width = this.height = side;
    }
}
这个答案只是为了展示完整的代码,完整的想法来自@runec


从设计角度看,我觉得构造函数Rectance(双倍宽度)对于矩形来说是不自然的,并且会将其删除。 正方形的构造函数应如下所示:

 public Square(double side) {
        super(side,side);   // width == height
        this.shapeName = "Square";
 }

为了更清楚地说明继承,您还可以替换行this.shapeName=“Rectangle”this.shapeName=getClass().getSimpleName()编写代码>并删除
this.shapeName=“Square”Square
的构造函数的code>。

只是为了澄清错误消息:

任何子类(例如,
Square
)的构造函数都必须调用其父类(
Rectangle
)的构造函数,因为父部分也必须构造。 如果这个构造函数不是由程序员提供的,也就是说,没有被显式调用(如在问题代码中),编译器会自动插入对父级的无参数构造函数的调用(如在
public Square(…){super();…}

在这个问题中,编译器会发送一个错误,因为父(
Rectangle
)没有这样的构造函数。因此,必须在代码中显式调用父类的构造函数,如(如已回答的):


您必须在
Square
构造函数中显式调用
Rectangle
的单参数构造函数。
 public Square(double side) {
        super(side,side);   // width == height
        this.shapeName = "Square";
 }
error: no suitable constructor found for Rectangle(no arguments)
    public Square(double side) {
                               ^
    constructor Rectangle.Rectangle(double) is not applicable
      (actual and formal argument lists differ in length)
    constructor Rectangle.Rectangle(double,double) is not applicable
      (actual and formal argument lists differ in length)
public Square(double side) {
    super(side,side);   // width == height
    ...