在下面的代碼中,我創建了 Movie 類的 3 個實體。我已經為這三個物件分配了一些資料。其次,我創建了對物件 movie1(movie4) 的第二個參考。我還為 movie4 的屬性賦值。我列印了電影 1 的新屬性,但內容沒有改變。為什么會發生這種情況?確切的原因是什么?我知道更新內容的其他方法,但我想知道為什么第二次參考不起作用。
內容是movie1還是一樣。為什么 ?
class Driver{
public static void main(String[] args){
Movie movie1 = new Movie("The Shawshank Redemption", 1994, 9.3);
Movie movie2 = new Movie("The Godfather", 1972, 9.2);
Movie movie3 = new Movie("The Dark Knight", 2008, 9.0);
**Movie movie4 = movie1;
movie4 = new Movie("The Return of the King", 2003, 8.9);**
movie4.name = "The Return of the King";
movie4.year_of_release = 2003;
movie4.rating = 8.9;
System.out.println("\n*********New Movie 1 Details*************\n");
movie1.printDetails();
}
}
uj5u.com熱心網友回復:
這movie4表明movie1:
Movie movie4 = movie1;
但是,然后您立即更新movie4以指向不同的物件:
movie4 = new Movie("The Return of the King", 2003, 8.9);
所以在這一點上,movie1和movie4不再指向相同的Movie,因此您通過的更改movie4不會影響movie1指向的物件。
如果您在沒有替換的情況下更改了物件movie4:
Movie movie4 = movie1;
movie4.name = "The Return of the King";
...那么無論您查看movie1.name或,您都會看到物件的變化movie4.name。
再詳細一點:
在你這樣做之后:
Movie movie1 = new Movie("The Shawshank Redemption", 1994, 9.3);
Movie movie2 = new Movie("The Godfather", 1972, 9.2);
Movie movie3 = new Movie("The Dark Knight", 2008, 9.0);
...你在記憶體中有這樣的東西(省略了很多細節):
????????????????????????????
電影 1:Ref4516???????->| (電影) |
????????????????????????????
| name: "肖申克..." |
| ... |
????????????????????????????
????????????????????????????
電影 2:Ref8469????????>| (電影) |
????????????????????????????
| 名稱:《教父》 |
| ... |
????????????????????????????
????????????????????????????
電影 3:Ref4789????????>| (電影) |
????????????????????????????
| 名稱:《黑暗騎士》 |
| ... |
????????????????????????????
然后你這樣做了:
Movie movie4 = movie1;
...制作movie4并movie1指向同一部??電影:
電影 1:Ref4516???
| ????????????????????????????
???>| (電影) |
| ????????????????????????????
電影 4:Ref4516??? | name: "肖申克..." |
| ... |
????????????????????????????
????????????????????????????
電影 2:Ref8469????????>| (電影) |
????????????????????????????
| 名稱:《教父》 |
| ... |
????????????????????????????
????????????????????????????
電影 3:Ref4789????????>| (電影) |
????????????????????????????
| 名稱:《黑暗騎士》 |
| ... |
????????????????????????????
到目前為止,一切都很好,但后來你做到了:
movie4 = new Movie("The Return of the King", /*...*/);
...movie4指向一個新物件:
??????????????????????????
movie1:Ref4516???????>| (Movie) |
??????????????????????????
| name: "The Shawshank..." |
| ... |
??????????????????????????
??????????????????????????
movie2:Ref8469???????>| (Movie) |
??????????????????????????
| name: "The Godfather" |
| ... |
??????????????????????????
??????????????????????????
movie3:Ref4789???????>| (Movie) |
??????????????????????????
| name: "The Dark Knight" |
| ... |
??????????????????????????
??????????????????????????
movie4:Ref9546???????>| (Movie) |
??????????????????????????
| name: "The Return of..." |
| ... |
??????????????????????????
So assigning to movie4.name just changes that new object, not the one that movie1 points to.
If you didn't do that movie4 = new Movie(/*...*/) bit, so movie1 and movie4 still pointed at the same object
電影 1:Ref4516???
| ????????????????????????????
???>| (電影) |
| ????????????????????????????
電影 4:Ref4516??? | name: "肖申克..." |
| ... |
????????????????????????????
????????????????????????????
電影 2:Ref8469????????>| (電影) |
????????????????????????????
| 名稱:《教父》 |
| ... |
????????????????????????????
????????????????????????????
電影 3:Ref4789????????>| (電影) |
????????????????????????????
| 名稱:《黑暗騎士》 |
| ... |
????????????????????????????
......然后分配給movie4.name將改變物件都movie4和movie1在指向:
movie4.name = "The Return of the King";
電影 1:Ref4516???
| ????????????????????????????
???>| (電影) |
| ????????????????????????????
電影 4:Ref4516??? | name: "...的歸來" |
| ... |
????????????????????????????
????????????????????????????
電影 2:Ref8469????????>| (電影) |
????????????????????????????
| 名稱:《教父》 |
| ... |
????????????????????????????
????????????????????????????
電影 3:Ref4789????????>| (電影) |
????????????????????????????
| 名稱:《黑暗騎士》 |
| ... |
????????????????????????????
uj5u.com熱心網友回復:
執行此命令后
Movie movie4 = movie1;
的值movie4是對 的參考movie1,因此對 所做的更改movie4將反映在movie1此時。
movie4 = new Movie("The Return of the King", 2003, 8.9);
但是在這個命令之后,movie4不再持有對 的參考movie1,它的值是一個全新的參考。所以所做的任何更改都只在movie4指向的新物件上進行,而不是在movie1
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/379549.html
