码迷,mamicode.com
首页 > 编程语言 > 详细

java:对象赋值

时间:2015-04-14 00:27:22      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

 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

 

java:对象赋值

标签:

原文地址:http://www.cnblogs.com/taoxiuxia/p/4423527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!