class Tank { int level; public Tank(int level){ this.level = level; } } public class Assignment { public static void main(String[] args) { Tank t1 = new Tank(9); Tank t2 = new Tank(47); System.out.println("t1.level: " + t1.level + ", t2.level: " + t2.level); //t1现在指向t2对象 t1 = t2; System.out.println("t1.level: " + t1.level + ", t2.level: " + t2.level); //修改t1的同时也修改了t2 t1.level = 36; System.out.println("t1.level: " + t1.level + ", t2.level: " + t2.level); //传递给f()函数的是t1的引用,而不是t1的副本 f(t1); System.out.println("t1.level:"+t1.level + ", t2.level: " + t2.level); } public static void f(Tank tank) { tank.level = 5; } } //如果只想改变t1的level值而不影响t2的值,可以这样调用 // t1.level = t2.level //但直接操作对象的属性违背面向对象程序设计的原则