我不太知道該怎麼稱呼它,但本質上,當我運行這段代碼:Java對象從另一個實例中獲取變量?
public class test {
static Device one;
static Device two;
public static void main(String[] args) throws Exception {
one = new Device("One", "ONE");
System.out.println(one.getName());
two = new Device("Two", "TWO");
System.out.println(one.getName());
System.out.println(two.getName());
}
}
輸出是:
ONE
TWO
TWO
當它應該是:
ONE
ONE
TWO
設備對象非常簡單,它只接收兩個字符串,第二個是我要求它打印的「名稱」。我之前做過OOP,但我覺得我只是忘記了一些重要的方面,但似乎無法弄清楚。任何幫助表示讚賞,謝謝!
這裏是設備的構造函數:
public Device(String iP, String Name) {
//Set the IP address
IP = iP;
//Set the device's name
name = Name;
// Set the string version of the device (for transmitting)
stringVersion = IP + ";" + name;
}
如果你可以打印設備的構造函數,它會幫助我們回答。 – BlackVegetable 2012-07-06 16:02:39
顯示整個設備類。 IP和名稱是靜態的嗎? – assylias 2012-07-06 16:05:27
是的,他們非常感謝你。我忘了那個......再次感謝! – kakonsan 2012-07-06 16:07:32