为对象赋值的测试
?
简单案列:
?
public class ObjectEvaluate{
public static void main(String[]args){
?Tank t1=new Tank();
?Tank t2=new Tank();
?t1.level=9;
?t2.level=47;
?System.out.println("1:t1.level="+t1.level+",t2.level="+t2.level);
?//System.out.println("1:t1="+t1.hashCode()+",t2="+t2.hashCode());
?t1=t2;
?System.out.println("2:t1.level="+t1.level+",t2.level="+t2.level);
?//System.out.println("2:t1="+t1.hashCode()+",t2="+t2.hashCode());
?t1.level=32;
? System.out.println("3:t1.level="+t1.level+",t2.level="+t2.level);
?//System.out.println("3:t1="+t1.hashCode()+",t2="+t2.hashCode());
}
}
?
/**内部类*/
?class Tank{
int level;
}
?
?
/** Output:
????? 1:t1.level=9,t2.level=47
??????2:t1.level=47,t2.level=47
????? 3:t1.level=32,t2.level=32
*/
?
?
注解:
?? 将一个对象赋值给另一个对象,实际是将“引用”从一个地方复制到另一个地方,所以上述案列t1=t2实际上都是指向原本只有t2指向的那个对象