clone:用於兩個對象有相同的內容時,進行復制操做。html
提示:Java中要想自定義類的對象能夠被複制,自定義類就必須實現Cloneable中的clone()方法。java
淺複製:另外一個對象用clone()方法對已經存在的對象進行復制的時候,會改變被複制對象裏面全部的內容,換句話說,淺複製就是對對象的複製出現了一些問題。ide
深複製:另外一個對象對已有的對象進行相同的內容複製的時候,不會改變原對象的內容this
什麼狀況下會出現淺複製呢?用下面的例子進行說明:code
學生類:htm
public class Student implements Cloneable { private String name; private int age; private Professor professor; 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 Professor getProfessor() { return professor; } public void setProfessor(Professor professor) { this.professor = professor; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", professor=" + professor + "]"; } public Object clone() throws CloneNotSupportedException{ return super.clone(); } }
教授類:對象
public class Professor { 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; } @Override public String toString() { return "Professor [name=" + name + ", age=" + age + "]"; } }
案例一: blog
public class ShadowCopy { public static void main(String[] args) { Professor p1 = new Professor(); p1.setName("Professor Zhang"); p1.setAge(30); Student s1 = new Student(); s1.setName("xiao ming"); s1.setAge(18); s1.setProfessor(p1); System.out.println(s1); try { Student s2 = (Student) s1.clone(); Professor p2 = s2.getProfessor(); p2.setName("Professor Li"); p2.setAge(45); s2.setProfessor(p2); System.out.println("複製後的:s1 = " + s1); System.out.println("複製後的:s2 = " + s2); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } }
運行結果1:get
1 Student [name=xiao ming, age=18, professor=Professor [name=Professor Zhang, age=30]] 2 複製後的:s1 = Student [name=xiao ming, age=18, professor=Professor [name=Professor Li, age=45]] 3 複製後的:s2 = Student [name=xiao ming, age=18, professor=Professor [name=Professor Li, age=45]]
s1的導師爲30歲的Professor Zhang,恰巧s2與s1同名同歲,可是s2的導師爲45歲的Professor Li,因而咱們直接複製s1給s2,再修改下s2的導師的信息。但是,問題出現了,當咱們修改了s2的導師後,s2的信息是正確的,可是s1的導師信息也跟着修改了。博客
案例二:
public class ShadowCopy { public static void main(String[] args) { Professor p1 = new Professor(); p1.setName("Professor Zhang"); p1.setAge(30); Student s1 = new Student(); s1.setName("xiao ming"); s1.setAge(18); s1.setProfessor(p1); System.out.println(s1); try { Student s2 = (Student) s1.clone(); s2.setName("xiao hong"); s2.setAge(17); Professor p2 = s2.getProfessor(); p2.setName("Professor Li"); p2.setAge(45); s2.setProfessor(p2); System.out.println("複製後的:s1 = " + s1); System.out.println("複製後的:s2 = " + s2); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } }
運行結果二:
1 Student [name=xiao ming, age=18, professor=Professor [name=Professor Zhang, age=30]] 2 複製後的:s1 = Student [name=xiao ming, age=18, professor=Professor [name=Professor Li, age=45]] 3 複製後的:s2 = Student [name=xiao hong, age=17, professor=Professor [name=Professor Li, age=45]]
此次,咱們在clone後,又修改了s2的name和age,從結果能夠看出,s1的name和age並無由於s2的修改而改變。
以上出現的兩種狀況,就是咱們所說的淺複製,但咱們實際上不但願出現這兩種問題,如何解決呢?那咱們必須就要進行深複製。接下來就是我要說的深複製
public class Professor 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; } @Override public String toString() { return "Professor [name=" + name + ", age=" + age + "]"; } public Object clone() throws CloneNotSupportedException{ return super.clone(); } }
public class Student implements Cloneable { private String name; private int age; private Professor professor; 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 Professor getProfessor() { return professor; } public void setProfessor(Professor professor) { this.professor = professor; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", professor=" + professor + "]"; } public Object clone() throws CloneNotSupportedException{ Student newStudent = (Student) super.clone(); newStudent.professor = (Professor) professor.clone(); return newStudent; } }
再次運行案例一:
1.Student [name=xiao ming, age=18, professor=Professor [name=Professor Zhang, age=30]] 2.複製後的:s1 = Student [name=xiao ming, age=18, professor=Professor [name=Professor Zhang, age=30]] 3.複製後的:s2 = Student [name=xiao ming, age=18, professor=Professor [name=Professor Li, age=45]]
再次運行案例二:
1 Student [name=xiao ming, age=18, professor=Professor [name=Professor Zhang, age=30]] 2 複製後的:s1 = Student [name=xiao ming, age=18, professor=Professor [name=Professor Zhang, age=30]] 3 複製後的:s2 = Student [name=xiao hong, age=17, professor=Professor [name=Professor Li, age=45]]
以上就是咱們所說的深複製。好好去領悟哦!
參考博客:https://www.cnblogs.com/acode/p/6306887.html