面向对象深浅拷贝问题求大神解答~

u012398875 2013-10-10 09:31:12
public class Person {
protected String name;
protected MyDate birthday;
private static int count = 0;

public Person(String name,MyDate birthday)
{
this.name = name;
this.birthday = birthday;
count++;
}
public Person(String name)
{
this(name,new MyDate());
}
public Person()
{
this("",new MyDate());
}
public Person(Person p)
{
this(p.name,new MyDate(p.birthday));
}
public void set(String name)
{
this.name = name;
}
public void set(MyDate birthday)
{
this.birthday = new MyDate(birthday);
}
public void set(String name,MyDate birthday)
{
this.set(name);
this.set(birthday);
}
public String getName()
{
return this.name;
}
public MyDate getBirthday()
{
return this.birthday;
}
public String toString()
{
return this.name+","+this.birthday.toString()+","+this.getAge()+"岁";
}
public int getAge(int year)
{
return year-this.birthday.getYear();
}
public int getAge()
{
return getAge(MyDate.getThisYear());
}
public int olderThen(Person p)
{
return p.birthday.getYear()-this.birthday.getYear();
}
public static void howMany()
{
System.out.println(count+"个Person对象");
}
public void finalize()
{
System.out.println("释放对象("+this.toString()+")");
count--;
}


public static void main(String args[])
{
Person p1 = new Person("李小明",new MyDate(1979,3,15));
Person p2 = new Person(p1);
p2.set(p2.getName().substring(0,2)+"dong");
MyDate d = p2.getBirthday();
d.set(d.getYear()+2,d.getMonth(),d.getDay());
p2.set(d);
Person.howMany();
System.out.println("p1: "+p1+", p2: "+p2);
System.out.println(p1.getName()+"比"+p2.getName()+"大"+p1.olderThen(p2)+"岁");
p1.finalize();
Person.howMany();

}

}



我的疑问是 我认为 Person p2 = new Person(p1); 既是通过 浅拷贝 建立一个新的 Person 类的 p2对象,其中p2的成员变量 String 指向 p1对象中的String 也就是我认为 p1与p2的String指向同一个String区域 如果我调用p2.set(p2.getName().substring(0,2)+"dong"); 我认为也会改变p1对象中的String 的值。但是结果并没有改变p1中String的值。
实验结果如下:

2个Person对象
p1: 李小明,1979年3月15日,30岁, p2: 李小dong,1981年3月15日,28岁
李小明比李小dong大2岁
释放对象(李小明,1979年3月15日,30岁)
1个Person对象

...全文
338 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
pbfordream 2013-10-16
  • 打赏
  • 举报
回复
string类型有个静态常量池的概念,是不可变的,举个例子 String s1= new String("abc"); String s2=s1; s1 ="123"; 这时候s1的值为123 ,s2为abc
u012398875 2013-10-16
  • 打赏
  • 举报
回复
同意九楼。。。。。。
专业酱油选手 2013-10-15
  • 打赏
  • 举报
回复
string类型有个静态常量池的概念,是不可变的,举个例子 String s1= new String("abc"); String s2=s1; s1 ="123"; 这时候s1的值为123 ,s2为abc
午夜咖啡男 2013-10-11
  • 打赏
  • 举报
回复
Java 里面对对象变量的赋值都是修改指针。String 类本身也没有提供修改自身字符串的方法。 p2.set 相当于将 p2 的成员 name 指向了一个新的字符串
Carl_Z 2013-10-11
  • 打赏
  • 举报
回复
String 不同于 char* 任何时候 String p; p=""; 的时候 p 原先指向的啥都不会变 p指向新的字符串 这和Int是一样的 int a=3; int b=a; b=4; a不会跟着b变 a还是3
tianma630 2013-10-11
  • 打赏
  • 举报
回复
lz没理解string在内存中的存储,看看这个 http://bbs.csdn.net/topics/390588654
中华雪碧 2013-10-11
  • 打赏
  • 举报
回复
String和一般的object不同,每个新String都会有新的地址。可以换StringBuffer或者其他类试试
ncist_jianeng 2013-10-11
  • 打赏
  • 举报
回复
string.substring 是产生新的string变量,原来的string没变
郑涛 2013-10-11
  • 打赏
  • 举报
回复
String 是final,维护一个字符数组,数组外部只能引用该数组的拷贝,所以引用改来改去,到最后自己看你引用什么
soton_dolphin 2013-10-10
  • 打赏
  • 举报
回复
我感觉应该是这样写 Person(Person p) this(p.getName(), new Date)
名字到底多长 2013-10-10
  • 打赏
  • 举报
回复
public class ThreadTest {

	public static void main(String[] args) {
		A a = new A();
		a.a = "adsfasdf";
		A b = new A(a);
		System.out.println(a.a==b.a);  // 打印true
		//a.a = "xxx";System.out.println(b.a); // b.a还是adsfasdf
	}
}

class A{
	String a;
	A(A c){
		a = c.a;
	}
	A(){}
}
写个试了下,感觉很像OS里面的页式管理内存方法,当只读的时候,JVM两个对象的成员a指向同一内存区域,当修改的时候,会新申请内存。 等大神解答。

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧