对象赋值

对一个对象进行操作的时, 我们真正的操作的是对对象的引用.所以倘若"将一个对象赋值给另一个对象,

实际是将"引用"从一个地方复制到另一个地方

 

public class Assignment {
	public static void main(String[] args) {
		Tank t1 = new Tank();
		Tank t2 = new Tank();

		t1.level = 5;
		t2.level = 47;

		System.out.println("1: t1.level: " + t1.level + ", t2.level: "
				+ t2.level);

		t1 = t2;
		System.out.println("2: t1.level: " + t1.level + ", t2.level: "
				+ t2.level);

		t1.level = 27;
		System.out.println("3: t1.level: " + t1.level + ", t2.level: "
				+ t2.level);
	}
}

class Tank {
	int level;
}

 

运行结果:

1: t1.level: 5, t2.level: 47
2: t1.level: 47, t2.level: 47
3: t1.level: 27, t2.level: 27

 

你可能感兴趣的:(对象)