Java 淺克隆、深克隆

淺克隆

只復制了基本數(shù)據(jù)類型和String數(shù)據(jù)類型以及對應的數(shù)組類型,其他引用數(shù)據(jù)類型只是復制了引用地址;

使用方式

實現(xiàn)Cloneable接口,然后重寫clone方法,調(diào)用super.clone()即可

public static class Person implements Cloneable {
    @Override
    protected Person clone() throws CloneNotSupportedException {
         return (Person) super.clone();
    }
}

例如

public static class Person implements Cloneable {
    public String name;
    public int age;
    public String[] names;
    public Baby baby;
    public ArrayList<String> names2 = new ArrayList<>();
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person{" + "name='" + name + '\'' + ", age=" + age + ", names=" + Arrays.toString(names) + ", baby=" + baby + ", names2=" + names2 + '}';
    }
    @Override
    protected Person clone() throws CloneNotSupportedException {
        return (Person) super.clone();
    }
}
public static class Baby implements Cloneable {
    public String name;
    public int age;
    public String[] names;
    public Baby(String name, int age, String[] names) {
        this.name = name;
        this.age = age;
        this.names = names;
    }
    @Override
    public String toString() {
        return "Baby{" + "name='" + name + '\'' + ", age=" + age + ", names=" + Arrays.toString(names) + '}';
    }
    @Override
    protected Baby clone() throws CloneNotSupportedException {
        return (Baby) super.clone();
    }
}
public static void main(String[] args) throws CloneNotSupportedException {
    Person person = new Person("person", 10);
    person.names = new String[]{"qq", "www"};
    person.baby = new Baby("baby", 20, new String[]{"qqqqq", "wwwww"});
    person.names2.add("1111");
    person.names2.add("2222");
    //  對克隆后的數(shù)據(jù)進行更改
    Person clone = person.clone();
    clone.name = "person1";
    clone.age = 23;
    clone.names2.add("3333");
    clone.names = new String[]{"111", "222"};

    clone.baby.name = "baby1";
    clone.baby.names = new String[]{"ttttt"};
    
    System.out.println(person.toString());
    System.out.println(clone.toString());
}

對應輸出

Person{name='person', age=10, names=[qq, www], baby=Baby{name='baby1', age=20, names=[ttttt]}, names2=[1111, 2222, 3333]}
Person{name='person1', age=23, names=[111, 222], baby=Baby{name='baby1', age=20, names=[ttttt]}, names2=[1111, 2222, 3333]}

結論

發(fā)現(xiàn),在對克隆的數(shù)據(jù)進行數(shù)據(jù)更改后:

  • 基本數(shù)據(jù)類型,String,基本數(shù)據(jù)類型和String對應的數(shù)組,復制了一份,更改不會影響原始數(shù)據(jù);
  • 引用類型數(shù)據(jù),只是復制了引用地址,對克隆后的引用數(shù)據(jù)類型操作,原始數(shù)據(jù)也會變,如測試中的Baby對象;

深克隆

對引用數(shù)據(jù)類型創(chuàng)建新的對象,并將屬性克隆過去;需要遞歸克隆引用數(shù)據(jù)類型;

實現(xiàn)方式

public static class Person implements Cloneable {
    @Override
    protected Person clone() throws CloneNotSupportedException {
        Person person = (Person) super.clone();
        person.baby = baby.clone();
        person.names2 = (ArrayList<String>) names2.clone();
        return person;
    }
}

例如

將上面淺克隆的例子中的clone方法改成

public static class Person implements Cloneable {
    @Override
    protected Person clone() throws CloneNotSupportedException {
        Person person = (Person) super.clone();
        person.baby = baby.clone();
        person.names2 = (ArrayList<String>) names2.clone();
        return person;
    }
}

輸出

Person{name='person', age=10, names=[qq, www], baby=Baby{name='baby', age=20, names=[qqqqq, wwwww]}, names2=[1111, 2222]}
Person{name='person1', age=23, names=[111, 222], baby=Baby{name='baby1', age=20, names=[ttttt]}, names2=[1111, 2222, 3333]}

結論

  • 引用數(shù)據(jù)類型創(chuàng)建了一份新的數(shù)據(jù),對克隆的數(shù)據(jù)更改,不會影響原始數(shù)據(jù);

注意

1. 必須實現(xiàn)Cloneable接口,否則調(diào)用clone方法拋出異常

Object的clone方法的實現(xiàn)

protected Object clone() throws CloneNotSupportedException {
   if (!(this instanceof Cloneable)) {
       throw new CloneNotSupportedException("Class " + getClass().getName() +" doesn't implement Cloneable");
   }
   return internalClone();
}

 /*
 * Native helper method for cloning.
 */
@FastNative
private native Object internalClone();

2. 深克隆時子類也要復寫clone方法

父類實現(xiàn)了clone方法,如果子類不覆寫,那么子類只能淺克隆;

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容