本帖最后由 张飞年 于 2012-10-6 20:42 编辑
- public class Test别名现象 {
- public static void main(String[] args) {
- Number n1 = new Number();
- Number n2 = new Number();
- n1.i = 10;
- n2.i = 20;
-
- System.out.println("1: n1.i: " + n1.i + ", n2.i: " + n2.i);
- n1 = n2;//看清楚这是个引用赋值
- System.out.println("2: n1.i: " + n1.i + ", n2.i: " + n2.i);
- n1.i = 30;//改变n1的值
- System.out.println("3: n1.i: " + n1.i + ", n2.i: " + n2.i);
- }
- }
- class Number{
- int i;
- }
- 结果:
- 1: n1.i: 10, n2.i: 20
- 2: n1.i: 20, n2.i: 20
- 3: n1.i: 30, n2.i: 30
复制代码 |