2012-03-07 85 views
0

我的問題是,您可以在房間類的實例化上設置房間參數,但是如何設置房間類的聚合功能的屬性,例如牆屬性和窗口屬性?因爲我寧願不使用setter?有另一種方法嗎?設置聚合功能

因爲我可以實例化房間,然後我必須單獨添加牆和窗的實例嗎?因爲我可以實例化3個房間,每個房間都有不同的窗戶和牆壁尺寸?

ROOM CLASS < AGGREGATED WALL ARRAY LIST < AGREGGATED WINDOWS ARRAY LIST 

感謝

+0

有些情況下是有用的。例如。 Room類的字段是什麼? – bpgergo 2012-03-07 13:09:50

+0

以及「聚合功能」是什麼意思? – 2012-03-07 13:14:55

回答

3

是的,構造函數。像

public class Room { 

    private List<Window> windows = new ArrayList<Window>(); 
    private List<Wall> walls = new ArrayList<Wall>(); 
    private List<Door> doors = new ArrayList<Door>(); 

    public Room(int windows, int walls, int doors){ 
     for(int i=0; i < windows; i++) 
      windows.add(new Window()); 
     //similarly for walls and Doors :) 
    } 

    //getters 
} 

從評論更新:該OP希望避免制定者,它原來的Windows可能是不同的類型。這似乎是某種測試能力的組成,has-a的事情。

我想你需要這個。 想出來!

注:我已經在此編輯器寫了這個,對概念理解的目的,代碼可能無法編譯

//you see rooms have walls, and many. So constructor takes List of 
//windows and doors in each wall. So, a List of walls that has List 
// (mixed bag) of windows and doors in each wall -- some may have no 
//window/door, then 2nd list will be empty. 
public class Room { 

    List<Wall> _walls = new ArrayList<Wall>(); 

    public Room(List<List<Openable>> walls){ 
     for(List<Openable> windowsOrDoors : walls){ 
      this._walls.add(new Wall(windowsOrDoors)); 
     } 
    } 
} 

//wall can have many doors and/or windows. We pass out mixed 
// bag list here, constructor will figure out how to keep 
//them in separate lists 
public class Wall { 

    List<Door> doors = new ArrayList<Door>(); 
    List<Window> windows = new ArrayList<Window>(); 

    public Wall(List<Openable> openables){ 
     for(Openable windowsOrDoor : openables){ 
      if(windowsOrDoor instanceOf Window) 
       this.windows.add(windowsOrDoors); 
      else 
       this.doors.add(windowsOrDoors); 
     } 
    } 
} 

//Window class, simple class -- it is of type Openable 
public class Window implements Openable{ 
    public Window(int w, int h){ 
     //do something 
    } 
} 

//Door class, simple class -- it is of type Openable 
public class Door implements Openable{ 
    public Door(int w, int h){ 
     //do something 
    } 
} 

//Openable -- a common interface to Window and Door, you can use 
//abstract class here and have getHeight, getWidth and other common 
//methods. This just for the purpose that if you wanted to add another 
//Window/Door class like `ArchedDoor` which has a `radius` as well, 
//you will just write a `ArchedDoor implements Openable` and pass into 
//the Room/Wall constructor. Nothing will break. 
public interface Openable{} 
+0

但是如果每個窗口都有不同的大小呢?將不得不與setter賭注集?因爲一個forloop會創建它們都一樣嗎? – user969729 2012-03-07 13:17:34

+0

看到你有最靈活的空間。 (: – Nishant 2012-03-07 13:34:55

+0

哈哈!現在你已經完全失去了我哈哈,但它看起來不錯!,你能幫我最後一個忙,並請評論一下嗎?所以我可以弄明白嗎?非常感謝您的時間! – user969729 2012-03-07 13:40:14

0

我不知道,我理解你,

,但你可以到房間的構造函數添加參數,然後開始在房間的牆壁用這些參數作爲參數其構造函數(你可以將參數添加到它們的構造以及)

你的構造可以是這個樣子:

public Room(WinSize[] windowsSizes, Color[] wallsColors){ 
    for(int i = 0 ; i < WinSize.length ; i++) 
    { 
     windows.add(new Window(windowsSizes[i])); 
    } 
} 

(比方說你有一些類,如使用winsize等)

0

一些上下文從這個問題缺失,但總的來說,我可以說,是的,你不會爲彙總,計算字段public的setter 。

通常情況下,您擁有更新計算字段的私有方法,並且這些方法是從構造函數和/或公用字段的setter中調用的(即,每次修改公用字段時都會更新計算的字段)。