爲什麼下面的代碼返回100 100 1 1 1
而不是100 1 1 1 1
?Java靜態方法參數
public class Hotel {
private int roomNr;
public Hotel(int roomNr) {
this.roomNr = roomNr;
}
public int getRoomNr() {
return this.roomNr;
}
static Hotel doStuff(Hotel hotel) {
hotel = new Hotel(1);
return hotel;
}
public static void main(String args[]) {
Hotel h1 = new Hotel(100);
System.out.print(h1.getRoomNr() + " ");
Hotel h2 = doStuff(h1);
System.out.print(h1.getRoomNr() + " ");
System.out.print(h2.getRoomNr() + " ");
h1 = doStuff(h2);
System.out.print(h1.getRoomNr() + " ");
System.out.print(h2.getRoomNr() + " ");
}
}
爲什麼它似乎通過酒店的價值doStuff()?
http://stackoverflow.com/questions/40480/is-java-pass-by-reference – polygenelubricants 2010-04-28 16:01:12
Java傳遞值。看到鏈接的問題。 (另外,'static'與它無關)。 – polygenelubricants 2010-04-28 16:02:18