【问题标题】:Need to set a variable in a class which is extended from another class. Or override the type需要在从另一个类扩展的类中设置一个变量。或者覆盖类型
【发布时间】:2018-03-15 05:05:28
【问题描述】:

为什么我一直未能通过此测试?

Test Cube(1.0, 1.0, 1.0) 设置类型为“Cube”,

宽、长、高各为1.0 测试反馈

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

你的:矩形、1.0、1.0、1.0

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

我还有一个非常简单的界面,由我的立方体实现。这只是为了找到音量。我可以以某种方式利用我的界面来覆盖类型吗?

在 StackOverflow 上找不到此特定问题的答案。

这是我的 Rectangle 类

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 

这是我的 Cube 类

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 

Rectangle 类来自我的 Shape 类,

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

【问题讨论】:

    标签: java overriding extends implements


    【解决方案1】:

    您的立方体类使用 super 调用矩形的构造函数,该构造函数又调用 形状类的构造函数带有字符串“矩形”,基本上就像你拥有的那样,立方体构造函数不允许你设置它的立方体类型。您需要明确使用setType 方法。

    您可以在立方体的构造函数中添加该行

    this.setType("Cube");
    

    应该工作(尚未测试)。

    【讨论】:

    • 你,是个圣人。我已经坚持了几个小时。现在看起来很明显....我怎么忽略了它?!?! Gahh.... 非常沮丧,但非常感谢,谢谢您,先生。
    猜你喜欢
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 2020-10-05
    • 1970-01-01
    相关资源
    最近更新 更多