java:对象赋值

 1 package exercise;
 2 
 3 class Tank
 4 {
 5     int level;
 6 }
 7 
 8 public class Assignment{
 9     public static void main(String[]args){
10         Tank t1=new Tank();
11         Tank t2=new Tank();
12         
13         t1.level=9;
14         t2.level=47;
15         
16         System.out.println("t1.level:"+t1.level+",t2.level:"+t2.level);
17         
18         t1=t2;
19         System.out.println("t1.level:"+t1.level+",t2.level:"+t2.level);
20         //when you assign "from one object to another" ,your are actually copying a reference
21         //from one place to another.
22         //this means that if you say c=d for objects,you end up with both c and d pointing to
23         //the object that, originally,only d pointed to .
24         
25         t1.level=27;
26         System.out.println("t1.level:"+t1.level+",t2.level:"+t2.level);
27     }
28 }

输出

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

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。