2016-03-18 47 views
0

我想要計算矩形類中矩形的面積,但我不能通過立方體類中的矩形區域。立方體的面積將通過添加矩形的面積來計算數組中有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()); 
     } 
    } 
} 
+2

對不起,你不明白你在問什麼。您需要顯示傳遞數組參數的「Cube」類的代碼。 –

+0

@arfa你是什麼意思,但我不能通過立方體類的矩形區域。 – user3437460

+0

它可能很清楚,如果你可以看看uml圖鏈接 – arfa

回答

0

更新您的代碼

sum= sum+r[i];

這個

sum = sum + r[i].getArea();

基本上你已經取得的0123數組。如果你想得到width,lengtharea你必須使用你的對象的獲得者爲你的sum得到int

+0

的'getArea()'方法中做了一些手動工作,謝謝它的工作! – arfa

+0

@arfa如果解決方案有效,我的帖子旁邊會有一個複選標記。點擊它。它顯示其他人可能與您的解決方案有效的問題相同。 (只是讓你知道,因爲你是新的) – wiredniko

+0

哦再次感謝 – arfa

相關問題