我想要計算矩形類中矩形的面積,但我不能通過立方體類中的矩形區域。立方體的面積將通過添加矩形的面積來計算數組中有6次。 the uml diagram looks like this我不能將一個數組參數傳遞給另一個類
我的矩形類
public class Rectangle extends Shape {
Scanner in = new Scanner(System.in);
int length;
int width;
Rectangle(String name, String unit, int length, int width) {
super(name, unit);
this.length = length;
this.width = width;
}
public int getwidth() {
return this.width;
}
public int getlength() {
return this.length;
}
public void setwidth(int width) {
this.width = width;
}
public void setlength(int length) {
this.length = length;
}
@Override
public void getInput() {
}
@Override
public int getArea() {
return this.width * this.length;
}
@Override
public void display() {
super.display();
System.out.println("length:" + this.length + "width:" + this.width);
System.out.println("area is" + getArea());
}
public class Cube extends Shape {
Rectangle[] r = new Rectangle[6];
Boolean solid;
String color;
Cube(String name, String unit, Boolean solid, String color) {
super(name, unit);
this.solid = solid;
this.color = color;
}
@Override
public int getArea() {
int sum = 0;
r[0] = new Rectangle("Rectangle", "Unit1", 10, 20);
r[1] = new Rectangle("Rectangle", "Unit2", 10, 20);
r[2] = new Rectangle("Rectangle", "Unit3", 10, 20);
r[3] = new Rectangle("Rectangle", "Unit4", 10, 20);
r[4] = new Rectangle("Rectangle", "Unit5", 10, 20);
r[5] = new Rectangle("Rectangle", "Unit6", 10, 20);
for (int i = 0; i < r.length; i++) {
sum = sum + r[i];
}
return sum;
}
@Override
public void getInput() {
}
@Override
public void display() {
super.display();
System.out.println("color:" + this.color + "solid:" + this.solid);
System.out.println("sum of cube" + getArea());
}
}
}
對不起,你不明白你在問什麼。您需要顯示傳遞數組參數的「Cube」類的代碼。 –
@arfa你是什麼意思,但我不能通過立方體類的矩形區域。 – user3437460
它可能很清楚,如果你可以看看uml圖鏈接 – arfa