需要在从另一类延伸的类中设置一个变量.或覆盖类型



为什么我要继续失败此测试?

测试立方体(1.0,1.0,1.0)将类型设置为"立方体",

宽度,长度和高度各为1.0测试反馈

预期:立方体,1.0、1.0、1.0

您的:矩形,1.0、1.0、1.0

我需要Cube类能够将其类型设置为立方体。现在,它似乎正在设置为矩形。我的主填充了一系列形状,然后我有根据每种形状的类型来计数不同类型形状的方法。当我的矩形类已经扩展形状时,我该如何将立方体定义为形状,但是我必须让我的立方体类扩展矩形。它必须是因为我还需要访问该区域以及长度和宽度。

我也有一个非常简单的接口,该接口由我的立方体实现。只是为了找到卷。我可以以某种方式利用我的界面覆盖类型吗?

找不到有关此特定问题的stackoverflow的答案。

这是我的矩形类

public class Rectangle extends Shape {
    protected double width;
    protected double length;
    public Rectangle(double width, double length) {
        super("Rectangle");
        setWidth(width);
        setLength(length);
    }// end ctr 
    public final double getWidth  () {return width; }
    public final double getLength () {return length;}
    public final void setWidth (double width) {
        if (width < 0) {
            System.out.println("Value could not be updated due to negative double.");
        }else
            this.width = width;
    }// end width setter 
    public final void setLength (double length) {
        if (length < 0) {
            System.out.println("Value could not be updated due to negative double.");
        }else
            this.length = length;
    }// end length setter 
    @Override
    public double area() {
        return length * width;
    }// end area method
    public double perimeter() {
        return 2 * (length + width);
    }// end perimeter method 
    @Override
    public String toString() {
        String str = "";
        str += String.format("%10s", "Rectangle:") + " ";
        str += "width: " + String.format("%.1f", width) + ", " + "length: " + String.format("%.1f", length);
        str += ", " + "area: " + String.format("%.2f", area() ) + ", ";
        str += "perimeter: " + String.format("%.2f", perimeter() );
        return str;
    }// end descriptor 
}// end rect class 

这是我的立方体类

public class Cube extends Rectangle implements Shape3D  {
    protected double height;
    public Cube (double width, double length, double height) {
        super(width, length);
        setHeight(height);
    }// end ctr
    public final double getHeight () {return height;} // end get height 
    public final void setHeight (double height) {
        if (height < 0) {
            System.out.println("Value could not be updated due to negative double.");
        }else
            this.height = height;
    }// end set height 
    @Override
    public double volume () {
        return super.area() * height;
    } // end volume method
     @Override
    public String toString() {
        String str = "";
        str += String.format("%10s", "Cube:") + " ";
        str += "width: " + String.format("%.1f", width) + ", " + "length: " + String.format("%.1f", length);
        str += ", " + "area: " + String.format("%.2f", area() ) + ", ";
        str += "perimeter: " + String.format("%.2f", perimeter() );
        str += ", height: " + String.format("%.1f", height );
        str += ", volume: " + String.format("%.1f", volume() );
        return str;
    }// end descriptor 
}// end cube class 

矩形类来自我的形状类,

public abstract class Shape {
    protected String type;
    public Shape (String type) {
        setType(type);
    }// end ctr
    public final String getType ()            {return type;     }
    public final void setType   (String type) {this.type = type;}
    public abstract double area (); // end prototype
    public double getArea () {return area();}
    @Override
    public String toString() {
        String str = "";
        str += type;
        return str;
    }// end descriptor 
}// end shape class

您的立方体类使用Super来调用Rectangle的构造函数Shape类的构造函数带有字符串"矩形",基本上是您所拥有的,立方体构造函数将不允许您设置其类型的立方体。您将需要明确使用setType方法。

您可以在Cube的构造函数中添加行

this.setType("Cube");

应该工作(尚未测试)。

相关内容

最新更新