[英]Need to set a variable in a class which is extended from another class. Or override the type
为什么我的考试一直失败?
Test Cube(1.0,1.0,1.0)将类型设置为“ Cube”,
宽度,长度和高度各为1.0测试反馈
预期:立方体,1.0,1.0,1.0
您的:矩形,1.0、1.0、1.0
我需要多维数据集类能够将其类型设置为多维数据集。 现在,它似乎将自己设置为Rectangle。 我的主要对象是一个形状数组,然后我有了一些方法,可以根据每种形状的类型对不同类型的形状进行计数。 当我的矩形类已经扩展了形状,但是我必须让我的Cube类扩展矩形时,如何将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
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
您的多维数据集类使用super调用矩形的构造函数,而矩形的构造函数又使用字符串“ Rectangle”调用Shape类的构造函数,基本上,只要您拥有它,多维数据集构造函数都将不允许您设置其多维数据集类型。 您将需要显式使用setType
方法。
您可以在多维数据集的构造函数中添加该行
this.setType("Cube");
并且它应该工作(未经测试)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.