需要在从另一个 class 扩展的 class 中设置一个变量。或者覆盖类型
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
我需要立方体 class 才能将其类型设置为立方体。现在它似乎将自己设置为矩形。我主要填充一个形状数组,然后我有方法根据每个形状的类型来计算不同类型的形状。当我的矩形 class 已经扩展形状,但我必须让我的立方体 class 扩展矩形时,我如何将立方体定义为形状。一定是因为我还需要访问该区域以及长度和宽度。
我的多维数据集也实现了一个非常简单的界面。只是为了找到音量。我能以某种方式利用我的界面来覆盖类型吗?
无法在 Whosebug 上找到此特定问题的答案。
这是我的矩形 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
这是我的魔方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
矩形 class 来自我的形状 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
您的多维数据集 class 使用 super 调用矩形的构造函数,后者又调用
使用字符串 "Rectangle" 构造 class 的构造函数,基本上就像您拥有的那样,立方体构造函数不允许您设置其立方体类型。您将需要明确使用 setType
方法。
您可以在多维数据集的构造函数中添加行
this.setType("Cube");
它应该 工作(尚未测试)。
为什么我总是无法通过此测试?
Test Cube(1.0, 1.0, 1.0) 设置类型为 "Cube",
宽、长、高各为1.0 测试反馈
预期:立方体,1.0、1.0、1.0
你的:矩形、1.0、1.0、1.0
我需要立方体 class 才能将其类型设置为立方体。现在它似乎将自己设置为矩形。我主要填充一个形状数组,然后我有方法根据每个形状的类型来计算不同类型的形状。当我的矩形 class 已经扩展形状,但我必须让我的立方体 class 扩展矩形时,我如何将立方体定义为形状。一定是因为我还需要访问该区域以及长度和宽度。
我的多维数据集也实现了一个非常简单的界面。只是为了找到音量。我能以某种方式利用我的界面来覆盖类型吗?
无法在 Whosebug 上找到此特定问题的答案。
这是我的矩形 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
这是我的魔方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
矩形 class 来自我的形状 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
您的多维数据集 class 使用 super 调用矩形的构造函数,后者又调用
使用字符串 "Rectangle" 构造 class 的构造函数,基本上就像您拥有的那样,立方体构造函数不允许您设置其立方体类型。您将需要明确使用 setType
方法。
您可以在多维数据集的构造函数中添加行
this.setType("Cube");
它应该 工作(尚未测试)。