我有一个类 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我怎么也弄不明白 怎么样 才能 避免这种问题啊

解决方案 »

  1.   

    This is called shallow clone.When an object instance is cloned, all instance variables are cloned by value. We have to consider the following situations:
    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
      

  2.   

    Client.javapackage test;public class Client {
    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());
    }
    }

    }
    英语看不懂
      

  3.   

    如果使用 clone 的方法,想做到深层克隆,就是我这对象里的所有属性都实手动实现克隆
    我觉得最好的,效率又高的方法使用对象系列化,它只要把每个类都标记上可序系化,就可以了
    很方便,克隆后,两个对象完全没有关系