原諒我,我是Java新手!併爲一個大學項目,我有幾個概念的麻煩,我試圖谷歌周圍無濟於事。如何在MarsRoom類中實例化一定數量的加熱器類加熱器,然後在MasterControlPanel的主體中訪問它們。但取決於在構造函數中設置的numheaters的數量??。我已經嘗試過,但它不識別對象room1.roomheaters [0]。但它確實承認它,如果我只是實例化一個對象,如加熱器roomheaters =新加熱器();. 非常感謝Java對象實例化
public class MasterControlPanel{
public static void main(String[] args){
MarsRoom room1 = new MarsRoom(40, 40, 20, 20, 8, 2, 4);
MarsRoom room2 = new MarsRoom(40, 40, 20, 20, 8, 2, 4);
MarsRoom room3 = new MarsRoom(40, 40, 20, 20, 8, 2, 4);
MarsRoom room4 = new MarsRoom(40, 40, 20, 20, 8, 2, 4);
room1.createheaters();
System.out.println("Turned " + (room1.roomheaters[0].getHeater() ? " ON" : " OFF"));
}
}
public class MarsRoom extends Rooms implements RoomInterface{
int roomareasq;
int heatloss;
float insideTemp;
float outsideTemp;
float uvalue;
int numheaters;
int numlights;
Heaters roomheaters[] = new Heaters[numheaters];
public MarsRoom(){
}
public MarsRoom(int windowsH, int windowsW, int wallsH, int wallsW, int windowC, int heaters, int lights){
windowsHeight = windowsH;
windowsWidth = windowsW;
wallsHeight = wallsH;
wallsWidth = wallsW;
windowCeiling = windowC;
numheaters = heaters;
numlights = lights;
}
public void createheaters(){
for (int i=0; i < numheaters; i++)
{
roomheaters[i] = new Heaters();
}
}
public void calculateheatloss(){
}
}
凡'roomheaters'宣佈進入加熱器? – talnicolas 2012-02-27 18:31:04
「不承認」是什麼意思?你是否收到NullPointerException或其他錯誤? – 2012-02-27 18:32:10