我有一个类 Client 理由 有4个属性 id name 分别都是StringBuffer 类型的
我先给他们赋值
Client cl = new Client();
cl.setId(new StringBuffer("2"));
cl.setName(new StringBuffer("ddd"));
cl.setRe(new StringBuffer("1"));
cl.setTel(new StringBuffer("2"));
然后 我克隆出一个对象 放到 po 里
Client po = (Client) cl.clone();
用str 引用 po.getName();
StringBuffer str = po.getName();
然后给str 赋值
str.append("fff");
下面同理
Client po1 = (Client) cl.clone();
StringBuffer str1 = po1.getName();
str1.append("eee");
list.add(po);
if(str == str1){
System.out.println("=======");
}
for (MyBasicPojo f1 : list) {
System.out.println(((Client)f1).getName());
}
最后控制台 输出的是
=======
dddfffeee
dddfffeee我怎么也弄不明白 怎么样 才能 避免这种问题啊
我先给他们赋值
Client cl = new Client();
cl.setId(new StringBuffer("2"));
cl.setName(new StringBuffer("ddd"));
cl.setRe(new StringBuffer("1"));
cl.setTel(new StringBuffer("2"));
然后 我克隆出一个对象 放到 po 里
Client po = (Client) cl.clone();
用str 引用 po.getName();
StringBuffer str = po.getName();
然后给str 赋值
str.append("fff");
下面同理
Client po1 = (Client) cl.clone();
StringBuffer str1 = po1.getName();
str1.append("eee");
list.add(po);
if(str == str1){
System.out.println("=======");
}
for (MyBasicPojo f1 : list) {
System.out.println(((Client)f1).getName());
}
最后控制台 输出的是
=======
dddfffeee
dddfffeee我怎么也弄不明白 怎么样 才能 避免这种问题啊
解决方案 »
- java 计算器
- 如何判断java运行时有多少个jframe??
- What method call is used to tell a thread that it has the opportunity to run?
- 关于数组赋值的问题:
- 各位达人,用 URLConnection 读网页,为什么有些部分读不出来?
- 使用type 4 driver(db2jcc.jar) 操作db2数据库出错………………急!!!!!
- 请问如何实现勾选 setcolor可以实现任一时刻的按钮颜色变换?
- 关于类继承的问题
- java能否调用SQL-SERVER存储过程呢?如果能?如何调?
- 大虾请进!急急急!
- 初学者关于字符串的问题
- 关于求众数的问题,找出数组中出现最多的数
1. If the instance variable is of a primitive type, the corresponding instance variable in the newly cloned instance has the same value as the existing instance.
2, If the instance variable is of a reference type, i.e. StringBuffer in your example, then the corresponding instance variable in the newly cloned instance has the same reference value as the existing instance. In this case, both reference variable refer to save Object instance.In your example, we have the second situation. c1.getName(), po.getName() and po1.getName() return a reference value referring to same StringBuffer instance.Are you clear now? :D
private StringBuffer id;
private StringBuffer name;
private StringBuffer re;
private StringBuffer tel;
public StringBuffer getId() {
return id;
}
public void setId(StringBuffer id) {
this.id = id;
}
public StringBuffer getName() {
return name;
}
public void setName(StringBuffer name) {
this.name = name;
}
public StringBuffer getRe() {
return re;
}
public void setRe(StringBuffer re) {
this.re = re;
}
public StringBuffer getTel() {
return tel;
}
public void setTel(StringBuffer tel) {
this.tel = tel;
}
public Client clone(){
Client c = new Client();
c.id = new StringBuffer(this.id.toString());
c.name = new StringBuffer(this.name.toString());
c.re = new StringBuffer(this.re.toString());
c.tel = new StringBuffer(this.tel.toString());
return c;
}
}
test.javaimport java.util.ArrayList;public class TestA {
public static void main(String[] args) { Client cl = new Client();
cl.setId(new StringBuffer("2"));
cl.setName(new StringBuffer("ddd"));
cl.setRe(new StringBuffer("1"));
cl.setTel(new StringBuffer("2")); Client po = (Client) cl.clone();
StringBuffer str = po.getName();
str.append("fff"); ArrayList<Client> list = new ArrayList<Client>();
Client po1 = (Client) cl.clone();
StringBuffer str1 = po1.getName();
str1.append("eee");
list.add(po);
list.add(po1);
if (str.equals(str1)) {
System.out.println("=======");
}
for (Client f1 : list) {
System.out.println(((Client) f1).getName());
}
}
}
英语看不懂
我觉得最好的,效率又高的方法使用对象系列化,它只要把每个类都标记上可序系化,就可以了
很方便,克隆后,两个对象完全没有关系