2016-07-02
1 deep copy: Not only copies the object, but also the contents of the address referenced by the object. Changing one property of an object does not affect what the other object refers to.
2 Shallow copy: Copies only the object itself, does not copy the referenced (referred to) content, and when changes are made to an object, the corresponding properties of the other object are changed as well.
3 deep Copy to override the Clone function. implements Cloneable.
Shallow copy:
Deep copy
This code is for reference (remember this, annotated---reference resources)
classProfessor0Implementscloneable {String name; intAge ; Professor0 (String name,intAge ) { This. Name =name; This. Age =Age ; } PublicObject Clone ()throwsclonenotsupportedexception {return Super. Clone ();//called by the parent class. }} classStudent0Implementscloneable {String name;//A constant object. intAge ; Professor0 p;//The reference value for student 1 and Student 2 is the same. Student0 (String name,intAge , Professor0 p) { This. Name =name; This. Age =Age ; This. P =p; } /*Public Object Clone () {Student0 o = null; try {o = (Student0) super.clone (); } catch (Clonenotsupportedexception e) {System.out.println (e.tostring ()); } return o; }*/} Public classqiancopy { Public Static voidMain (string[] args) {Professor0 P=NewProfessor0 ("Perfesser0", 50); Student0 S1=NewStudent0 ("STU1", 18, p); System.out.println ("Student S1 Name:" + s1.name + "\ n student S1 professor's name:" + S1.p.name + "," + "\ nthe student S1 professor's age" + s1.p.age+ "... end");//Professor of Student 1Student0 S2=S1; S2.p.name= "S2copys"; S2.p.age= 30; S2.name= "S2"; S2.age= 45; System.out.println ("Student S1 Name:" + s1.name+ "\ n student S1 Age:" + s1.age + "\ n student S1 professor's name:" + S1.p.name + "," + "\ n Student S1 professor's age" + s1.p.age+ "... end"); System.out.println ("Student S2 Name:" + s2.name+ "\ n student s2 Age:" + s2.age + "\ n Student S2 professor's name:" + S2.p.name + "," + "\ n Student S2 professor's age" + s2.p.age+ "... end");//Professor of Student 1 }}
classProfessorImplementscloneable {String name; intAge ; Professor (String name,intAge ) { This. Name =name; This. Age =Age ; } PublicObject Clone () {object o=NULL; Try{o=Super. Clone (); } Catch(clonenotsupportedexception e) {System.out.println (e.tostring ()); } returno; }} classStudentImplementscloneable {String name; intAge ; Professor P; Student (String name,intAge , Professor P) { This. Name =name; This. Age =Age ; This. P =p; } PublicObject Clone () {Student o=NULL; Try{o= (Student)Super. Clone ();//stu Deep Copy}Catch(clonenotsupportedexception e) {System.out.println (e.tostring ()); } O.P= (professor) P.clone ();//professer Deep Copy returno; }} Public classshencopy { Public Static voidMain (String args[]) {Professor P=NewProfessor ("Professer1", 50); Student S1=NewStudent ("STU1", 18, p); System.out.println ("Student S1 Name:" + s1.name+ "\ n student S1 Age:" + s1.age + "\ n student S1 professor's name:" + S1.p.name + "," + "\ n Student S1 professor's age" + s1.p.age+ "... end");//Professor of Student 1Student S2=(Student) S1.clone (); S2.name= "S2copys"; S2.age= 30; S2.p.name= "STU2"; S2.p.age= 30; System.out.println ("Student S1 Name:" + s1.name+ "\ n student S1 Age:" + s1.age + "\ n student S1 professor's name:" + S1.p.name + "," + "\ n Student S1 professor's age" + s1.p.age+ "... end");//Professor of Student 1System.out.println ("Student S2 's name:" + s2.name+ "\ n student s2 Age:" + s2.age + "\ n Student S2 professor's name:" + S2.p.name + "," + "\ n Student S2 professor's age" + S2.p.a Ge+ "... end");//Professor of Student 2 }}
Java depth Copy