1. Definition: Know an object, but do not know the class, want to get the same copy of the object, when modifying the properties of the object, the Copy property is not modified, clone is the property of the object
2. Meaning: When a lot of properties in an object, want to get an identical object, and set a lot of properties is very troublesome
3. Implementation: Implementation of the Cloneable interface (identity interface, there is no method definition) identifies that the class object can be cloned
Override the Clone () method in object to copy the class instance (object) by field, legally (implementing the above interface).
In the overridden Clone () method, return Super.clone (); (called the parent class (Object) of the Clone method, in fact, the subclass did nothing) copy the properties of the current object with the clone of the parent class to return the copied object
4. Deep copy, Shallow copy: Object attributes have primitive type int String (not basic but final), etc., reference variable (object of other Class), because copy of basic basic type variable is value copy, copy of reference variable is copy address, so the reference variable of the cloned object is the same variable, When one object is modified, another object is modified, so there is a deep copy. Implementing a deep copy requires the class of the object referencing the variable to also implement the Cloneable interface, from the Write Clone () method, return Super.clone ();
5. Deep copy seems to be used on the Java framework Hibernate
/*Shallow copy s modified teacher S1 teacher also modified the*/ Packageweiguoyuan.chainunicom.cn;classTeacher1 {PrivateString name; PublicTeacher1 (String name) { This. Name =name; } Public voidsetName (String name) { This. Name =name; } PublicString toString () {returnname; }}classStudent1Implementscloneable{PrivateString name; PrivateTeacher1 teacher; PublicStudent1 (String name,teacher1 teacher) { This. Name =name; This. Teacher =teacher; } PublicTeacher1 Getteacher () {returnteacher; } PublicString toString () {returnname+ "" +teacher.tostring (); } PublicObject Clone ()throwsclonenotsupportedexception{return Super. Clone (); }} Public classTestclone { Public Static voidMain (string[] args)throwsclonenotsupportedexception {Teacher1 T=NewTeacher1 ("Jianghongweisb"); Student1 s=NewStudent1 ("WEIGUOYUANNB", T); Student1 S1=(STUDENT1) S.clone (); System.out.println (s); System.out.println (S1); S.getteacher (). SetName ("Jianghongwei"); System.out.println (s); System.out.println (S1); }}
/*** Deep Copy implementation*/ Packageweiguoyuan.chainunicom.cn;classTeacher1Implementscloneable{PrivateString name; PublicTeacher1 (String name) { This. Name =name; } Public voidsetName (String name) { This. Name =name; } PublicString toString () {returnname; } PublicObject Clone ()throwsclonenotsupportedexception{return Super. Clone (); }}classStudent1Implementscloneable{PrivateString name; PrivateTeacher1 teacher; PublicStudent1 (String name,teacher1 teacher) { This. Name =name; This. Teacher =teacher; } PublicTeacher1 Getteacher () {returnteacher; } PublicString toString () {returnname+ "" +teacher.tostring (); } PublicObject Clone ()throwsclonenotsupportedexception{Student1 Student= (STUDENT1)Super. Clone (); Student.teacher= (Teacher1) This. Teacher.clone (); returnstudent; }} Public classTestclone { Public Static voidMain (string[] args)throwsclonenotsupportedexception {Teacher1 T=NewTeacher1 ("Jianghongweisb"); Student1 s=NewStudent1 ("WEIGUOYUANNB", T); Student1 S1=(STUDENT1) S.clone (); System.out.println (s); System.out.println (S1); S.getteacher (). SetName ("Jianghongwei"); System.out.println (s); System.out.println (S1); }}
Clone deep Copy Shallow copy