[英]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.