class ReferencesTest {
public static void main(String[] arguments) {
Point pt1, pt2;
pt1 = new Point(100, 100);
pt2 = pt1; pt1.x = 200;
pt1.y = 200;
System.out.println("Point1: " + pt1.x + ", " + pt1.y);
System.out.println("Point2: " + pt2.x + ", " + pt2.y);
}
}
答案:Point1: 200, 200
Point2: 200, 200
为什么不是Point1: 200, 200
Point2: 100,100呢,
Point pt1, pt2;
pt1 = new Point(100, 100);
pt2 = pt1; pt1.x = 200;
pt1.y = 200;
System.out.println("Point1: " + pt1.x + ", " + pt1.y);
System.out.println("Point2: " + pt2.x + ", " + pt2.y);
等于Point pt1, pt2;
pt1 = new Point(100, 100);
pt1.x = 200;
pt1.y = 200;
pt2 = pt1;
System.out.println("Point1: " + pt1.x + ", " + pt1.y);
System.out.println("Point2: " + pt2.x + ", " + pt2.y);么?????
public static void main(String[] arguments) {
Point pt1, pt2;
pt1 = new Point(100, 100);
pt2 = pt1; pt1.x = 200;
pt1.y = 200;
System.out.println("Point1: " + pt1.x + ", " + pt1.y);
System.out.println("Point2: " + pt2.x + ", " + pt2.y);
}
}
答案:Point1: 200, 200
Point2: 200, 200
为什么不是Point1: 200, 200
Point2: 100,100呢,
Point pt1, pt2;
pt1 = new Point(100, 100);
pt2 = pt1; pt1.x = 200;
pt1.y = 200;
System.out.println("Point1: " + pt1.x + ", " + pt1.y);
System.out.println("Point2: " + pt2.x + ", " + pt2.y);
等于Point pt1, pt2;
pt1 = new Point(100, 100);
pt1.x = 200;
pt1.y = 200;
pt2 = pt1;
System.out.println("Point1: " + pt1.x + ", " + pt1.y);
System.out.println("Point2: " + pt2.x + ", " + pt2.y);么?????
pdskiller(害虫) ( ) 信誉:100 Blog 2006-11-30 17:14:56 得分: 0
Point pt1, pt2;//不分配内存空间
pt1 = new Point(100, 100);// 给pt1分配内存空间
pt2 = pt1;//pt2的地址=pt1的地
Top
pdskiller(害虫) ( ) 信誉:100 Blog 2006-11-30 17:23:15 得分: 0
可能我说的你不太明白,但意思就是这个意思,pt2只是引用了一下pt1而已,并没有重新分配内存。
Top
Point pt1, pt2;//不分配内存空间
pt1 = new Point(100, 100);// 给pt1分配内存空间
pt2 = pt1;//pt2的地址=pt1的地址,即两者同时指向同1个内存位置
/**
此时pt2引用的是pt1
pt2.x就是pt1.x,即100
*/
---------------------------------------------------------------
pt1.x = 200;
pt1.y = 200;
/**
此时pt2引用的还是pt1
pt1变了
pt2.x就是pt1.x,即200,原来100那个内存位置,已变成了200
*/