Java的深拷貝和淺拷貝的區別

2023-01-09 21:00:22

一、拷貝的引入



(1)、參照拷貝


建立一個指向物件的參照變數的拷貝。


Teacher teacher = new Teacher("Taylor",26);
Teacher otherteacher = teacher;
System.out.println(teacher);
System.out.println(otherteacher);


輸出結果:


blog.Teacher@355da254
blog.Teacher@355da254


結果分析:由輸出結果可以看出,它們的地址值是相同的,那麼它們肯定是同一個物件。teacher和otherteacher的只是參照而已,他們都指向了一個相同的物件Teacher(「Taylor」,26)。這就叫做參照拷貝。


image.png


(2)、物件拷貝


建立物件本身的一個副本。


Teacher teacher = new Teacher("Swift",26); 
Teacher otherteacher = (Teacher)teacher.clone(); 
System.out.println(teacher);
System.out.println(otherteacher);


輸出結果:


blog.Teacher@355da254
blog.Teacher@4dc63996


image.png


注:深拷貝和淺拷貝都是物件拷貝


二、淺拷貝



(1)、定義


被複制物件的所有變數都含有與原來的物件相同的值,而所有的對其他物件的參照仍然指向原來的物件。即物件的淺拷貝會對「主」物件進行拷貝,但不會複製主物件裡面的物件。」裡面的物件「會在原來的物件和它的副本之間共用。


簡而言之,淺拷貝僅僅複製所考慮的物件,而不復制它所參照的物件


(2)、淺拷貝範例

package com.test;

public class ShallowCopy {
    public static void main(String[] args) throws CloneNotSupportedException {
        Teacher teacher = new Teacher();
        teacher.setName("riemann");
        teacher.setAge(27);

        Student2 student1 = new Student2();
        student1.setName("edgar");
        student1.setAge(18);
        student1.setTeacher(teacher);

        Student2 student2 = (Student2) student1.clone();
        System.out.println("拷貝後");
        System.out.println(student2.getName());
        System.out.println(student2.getAge());
        System.out.println(student2.getTeacher().getName());
        System.out.println(student2.getTeacher().getAge());

        System.out.println("修改老師的資訊後-------------");
        // 修改老師的資訊
        teacher.setName("Games");
        System.out.println(student1.getTeacher().getName());
        System.out.println(student2.getTeacher().getName());

    }
}

class Teacher implements Cloneable {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Student2 implements Cloneable {
    private String name;
    private int age;
    private Teacher teacher;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public Object clone() throws CloneNotSupportedException {
        Object object = super.clone();
        return object;
    }
}

輸出結果:


拷貝後
edgar
18
riemann
27
修改老師的資訊後-------------
Games
Games


結果分析:兩個參照student1和student2指向不同的兩個物件,但是兩個參照student1和student2中的兩個teacher參照指向的是同一個物件,所以說明是淺拷貝。


三、深拷貝



(1)、定義


深拷貝是一個整個獨立的物件拷貝,深拷貝會拷貝所有的屬性,並拷貝屬性指向的動態分配的記憶體。當物件和它所參照的物件一起拷貝時即發生深拷貝。深拷貝相比於淺拷貝速度較慢並且花銷較大。


簡而言之,深拷貝把要複製的物件所參照的物件都複製了一遍。


(2)、深拷貝範例

package com.test;

public class DeepCopy {
    public static void main(String[] args) throws CloneNotSupportedException {
        Teacher2 teacher = new Teacher2();
        teacher.setName("riemann");
        teacher.setAge(27);

        Student3 student1 = new Student3();
        student1.setName("edgar");
        student1.setAge(18);
        student1.setTeacher(teacher);

        Student3 student2 = (Student3) student1.clone();
        System.out.println("拷貝後");
        System.out.println(student2.getName());
        System.out.println(student2.getAge());
        System.out.println(student2.getTeacher().getName());
        System.out.println(student2.getTeacher().getAge());

        System.out.println("修改老師的資訊後-------------");
        // 修改老師的資訊
        teacher.setName("Games");
        System.out.println(student1.getTeacher().getName());
        System.out.println(student2.getTeacher().getName());
    }
}

class Teacher2 implements Cloneable {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Student3 implements Cloneable {
    private String name;
    private int age;
    private Teacher2 teacher;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Teacher2 getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher2 teacher) {
        this.teacher = teacher;
    }

    public Object clone() throws CloneNotSupportedException {
        // 淺複製時:
        // Object object = super.clone();
        // return object;

        // 改為深複製:
        Student3 student = (Student3) super.clone();
        // 本來是淺複製,現在將Teacher物件複製一份並重新set進來
        student.setTeacher((Teacher2) student.getTeacher().clone());
        return student;

    }
}


輸出結果:


拷貝後
edgar
18
riemann
27
修改老師的資訊後-------------
Games
riemann


結果分析:


兩個參照student1和student2指向不同的兩個物件,兩個參照student1和student2中的兩個teacher參照指向的是兩個物件,但對teacher物件的修改只能影響student1物件,所以說是深拷貝。


關於Java深拷貝和淺拷貝區別,你學廢了麼?

  • 區別:如果在拷貝這個物件的時候,只對基本資料型別進行了拷貝,而對參照型別資料只是進行了參照的傳遞,而沒有真實的建立一個新的物件,則認為是淺拷貝。反之,在對參照型別資料進行拷貝的時候,建立了一個新的物件,並且複製其內的成員變數,則認為是深拷貝。

問:clone()方法,是對當前物件進行淺拷貝,參照型別依然是在傳遞參照,那麼,如何進行一個深拷貝呢?

  • 序列化這個物件,再反序列化回來,就可得到這個新的物件,無非就是序列化的規則需要自己來寫。
  • 繼續利用clone()方法,既然clone()方法是我們來重寫的,實際上可以對其內的參照型別的變數,在進行一次clone()。