简体   繁体   English

需要在一个从另一个类扩展的类中设置一个变量。 或覆盖类型

[英]Need to set a variable in a class which is extended from another class. Or override the type

Why do I keep failing this test? 为什么我的考试一直失败?

Test Cube(1.0, 1.0, 1.0) sets the type to "Cube", Test Cube(1.0,1.0,1.0)将类型设置为“ Cube”,

width, length and height each to 1.0 Test feedback 宽度,长度和高度各为1.0测试反馈

Expected: Cube, 1.0, 1.0, 1.0 预期:立方体,1.0,1.0,1.0

Yours: Rectangle, 1.0, 1.0, 1.0 您的:矩形,1.0、1.0、1.0

I need the cube class to be able to set its type to Cube. 我需要多维数据集类能够将其类型设置为多维数据集。 Right now it seems to be setting itself to Rectangle. 现在,它似乎将自己设置为Rectangle。 My main fills an Array of Shapes, I then have methods to count the different types of shapes according to the Type of each shape. 我的主要对象是一个形状数组,然后我有了一些方法,可以根据每种形状的类型对不同类型的形状进行计数。 How can I define Cube as a shape, when my rectangle class already extends shape, but I MUST have my Cube class extend rectangle. 当我的矩形类已经扩展了形状,但是我必须让我的Cube类扩展矩形时,如何将Cube定义为形状。 It must because I need to have access to the area as well and the length and width. 一定是因为我需要访问该区域以及长度和宽度。

I also have a very simple interface that is implemented by my cube. 我还有一个非常简单的界面,该界面由我的多维数据集实现。 It is just to find the volume. 只是找到音量。 Can I somehow utilize my interface to override the type? 我可以以某种方式利用我的界面来覆盖类型吗?

Can't find an answer on StackOverflow to this specific question. 在StackOverflow上找不到此特定问题的答案。

Here is my Rectangle class 这是我的矩形课

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 

Here is my Cube 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 

The Rectangle class comes from my Shape 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

Your cube class uses super to call rectangle's constructor which in turn calls the Shape class's constructor with the string "Rectangle", basically as you have it, the cube constructor will not allow you to set its type of cube. 您的多维数据集类使用super调用矩形的构造函数,而矩形的构造函数又使用字符串“ Rectangle”调用Shape类的构造函数,基本上,只要您拥有它,多维数据集构造函数都将不允许您设置其多维数据集类型。 You will need to explicitly use the setType method. 您将需要显式使用setType方法。

You could in cube's constructor add the line 您可以在多维数据集的构造函数中添加该行

this.setType("Cube");

and it should work (have not tested). 并且它应该工作(未经测试)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM