猿问

如何设计一个继承自 Rectangle 类的 Square 类

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


> Square s = new Square(5);

> s.toString();

< Square with area 25.00 and perimeter 20.00

以下是我的代码:


abstract class Shape {

    protected String shapeName;


    public abstract double getArea();

    public abstract double getPerimeter();


    @Override

    public String toString() {

        return shapeName + " with area " + String.format("%.2f", getArea()) +

            " and perimeter " + String.format("%.2f", getPerimeter());

    }

}


class Rectangle extends Shape {

    protected double width;

    protected double height;


    public Rectangle(double width) {

        this.shapeName = "Rectangle";

        this.width = this.height = width;

    }


    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);

    }

}


class Square extends Rectangle {


    public Square(double side) {

        this.shapeName = "Square";

        this.width = this.height = side;

    }

}

问题是当我尝试编译它时,会发生此错误:


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 类中,因为代码以其他方式编译。


牧羊人nacy
浏览 266回答 3
3回答

SMILET

在设计方面,我觉得构造函数 Rectance( double width ) 对于矩形来说是不自然的,会删除它。Square 的构造函数应如下所示:&nbsp;public Square(double side) {&nbsp; &nbsp; &nbsp; &nbsp; super(side,side);&nbsp; &nbsp;// width == height&nbsp; &nbsp; &nbsp; &nbsp; this.shapeName = "Square";&nbsp;}为了进一步阐明继承,您还可以this.shapeName= "Rectangle";使用from的构造函数替换this.shapeName= getClass().getSimpleName();并删除该行。this.shapeName = "Square";Square

MYYA

您必须显式调用super继承类中的构造函数。如果你想考虑@runec 的回答(是的,我赞成它,因为它很好),你可能想删除只有一个参数的构造函数,Rectangle并使Square句柄成为一个有 4 个等边的矩形。public class Rectangle extends Shape {&nbsp; &nbsp; protected double width;&nbsp; &nbsp; protected double height;&nbsp; &nbsp; public Rectangle(double width, double height) {&nbsp; &nbsp; &nbsp; &nbsp; this.shapeName = "Rectangle";&nbsp; &nbsp; &nbsp; &nbsp; this.width = width;&nbsp; &nbsp; &nbsp; &nbsp; this.height = height;&nbsp; &nbsp; }&nbsp; &nbsp; public double getArea() {&nbsp; &nbsp; &nbsp; &nbsp; return width * height;&nbsp; &nbsp; }&nbsp; &nbsp; public double getPerimeter() {&nbsp; &nbsp; &nbsp; &nbsp; return 2 * (width + height);&nbsp; &nbsp; }}这里你有Square只需要一个double参数的 :public class Square extends Rectangle {&nbsp; &nbsp; public Square(double side) {&nbsp; &nbsp; &nbsp; &nbsp; super(side, side);&nbsp; &nbsp; &nbsp; &nbsp; this.shapeName = "Square";&nbsp; &nbsp; &nbsp; &nbsp; this.width = this.height = side;&nbsp; &nbsp; }}这个答案只是为了展示完整的代码,完整的想法来自@runec。
随时随地看视频慕课网APP

相关分类

Java
我要回答