|
先谢谢yining的提醒
给变量或者对象赋值和引用是一个比较容易混淆的东西
上面的例子,首先创建一个Number的类,然后在main里,创建了Number类的两个新的实例,通过new给这两个新的实例在内存空间给其分配地址,
第一次分别给两个实例分别赋值,各自贮存在自己获得的内存空间
然后,n1=n2; 实际上是令n1指向n2的内存地址,这样调用n1的值的时候,实际取得的是n2内存所存贮的值
第三次给n1.i赋值,实际上是给n2赋值,因为n1只是n2的一个引用
不知道我以上的理解对不对?
以下为原文
the number class is simple,and two instance of it (n1 and n2)are created witnin main().the i value within each Number is given a different value,and then n2 is assigned to n1,and n1 is changed,which is a reference to n2. |
|