First, what is deep replication? What is shallow copy?
Baidu told me--------------->
Shallow copy: After copying an object, the base type is recreated, and the reference type points to the reference referred to by the original object;
Deep copy: When an object is copied, both the base type and the reference type are recreated.
Here's a look at our deep copy method
Package Everyworkdayprogramming._2015_3_03;import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.io.objectinputstream;import Java.io.objectoutputstream;import Java.io.serializable;import java.util.date;/** * * When using object flow to deeply copy an object, the object must implement the Cloneable,serializable interface, which indicates that the object can be copied, while the latter is intended to satisfy our deep copy of the object using the object flow * */public class Copysource implements Cloneable, Serializable {/** * @Fields Serialversionuid: Serialization of an object uniquely identifies */private static final long ser Ialversionuid = -3446490540857082055l;/* defines a variable, not a base type, and implements the serializable interface */private String string;private Serializableobject serializableobject;public String getString () {return string;} public void SetString (string string) {this.string = string;} Public Serializableobject Getserializableobject () {return serializableobject;} public void Setserializableobject (Serializableobject serializableobject) {this.serializableobject = Serializableobject;} /* Shallow copy */public Object clone () throws Clonenotsupportedexception {/* Shallow copy directly invokes the Clone method of the parent class to replicate */copysource Copysource = (copysource) super.clone (); return copysource;} /* Deep Copy */public Object Deepclone () throws IOException, ClassNotFoundException {/* Write binary stream of current object */bytearrayoutputstream out = new Bytearrayoutputstream (), ObjectOutputStream oout = new ObjectOutputStream (out), Oout.writeobject (this);/* read out the new object generated by the binary stream */objectinputstream oin = ObjectInputStream (New Bytearrayinputstream (Out.tobytearray ()));/* So our deep copy work is done; all objects are new */return oin.readobject ();}} /** * * This class is contained in Copysource, which is used to test whether the object in the instance of the class is deeply copied * * */class Serializableobject implements Serializable {/** * @Fields s Erialversionuid: The serialization of an object uniquely identifies */private static final long Serialversionuid = 6357352492173107445l;/* Properties are used to test whether objects in objects can also be deeply copied *//* base type */private int x = 1;/* non-basic type */private Date date;private SerializableObject1 Seria lizableobject1;public int GetX () {return x;} public void SetX (int x) {this.x = x;} Public Date getDate () {return date;} public void setDate (date date{this.date = date;} Public SerializableObject1 GetSerializableObject1 () {return serializableObject1;} public void SetSerializableObject1 (SerializableObject1 serializableObject1) {This.serializableobject1 = SerializableObject1;}} /** * * Object objects in objects, of course, also implemented Serializable * * */class SerializableObject1 implements Serializable {/** * @Fields Serialversi Onuid: The serialization of an object uniquely identifies */private static final long serialversionuid = 1L;}
Test method:
Package Everyworkdayprogramming._2015_3_03;import Java.io.ioexception;import Java.util.date;public class Test { public static void Main (string[] args) {/* Creates an object to be copied and initializes the corresponding property */copysource copy = new Copysource (); copy.setserializableob Ject (New Serializableobject ()); Copy.setstring ("123"); Copy.getserializableobject (). SetX (2); Copy.getserializableobject (). SetDate (New Date ()); Copy.getserializableobject (). SetSerializableObject1 (New SerializableObject1 ()), try {/* Shallow copy Copy to copy1 */copysource copy1 = (copysource) copy.clone ();/* deep copy to Copy2 */copysou Rce copy2 = (copysource) copy.deepclone ();/* Determines whether serialiableobject is equal to the original object after shallow and deep copy */system.out.println ("---------- ------------------------------------------------determine if the serialiableobject is equal to the original object after shallow copy and deep copy. System.out.println (copy.getserializableobject () = = Copy1.getserializableobject ()); System.out.println (copy.getserializableobject () = = Copy2.getserializableobject ());/* Determines whether the string is equal to the original object after shallow copy and deep copy */ System.out.println ("----------------------------------------------------------determine if the string is equal to the original object after shallow copy and deep copy); System.out.println (copy.getstring () = = Copy1.getstring ()); System.out.println (copy.getstring () = = Copy2.getstring ());/* Determines whether date in Serializableobject is equal to the original object after shallow copy and deep copy */ System.out.println ("---------------------------------------------------------- Determine if the date in Serializableobject is equal to the original object after shallow copy and deep copy. System.out.println (Copy.getserializableobject (). GetDate () = = Copy1.getserializableobject (). GetDate ()); System.out.println (Copy.getserializableobject (). GetDate () = = Copy2.getserializableobject (). GetDate ());/* Determines whether SerializableObject1 in Serializableobject is equal to the original object after shallow copy and deep copy */system.out.println ("------------------------------ ----------------------------determine if the SerializableObject1 in Serializableobject is equal to the original object after shallow copy and deep copy. System.out.println (Copy.getserializableobject (). GetSerializableObject1 () = = Copy1.getserializableobject (). GetSerializableObject1 ()); System.out.println (Copy.getserializableobject (). GetSerializableObject1 () = = Copy2.getserializableobject (). GetseriaLizableObject1 ());} catch (Clonenotsupportedexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} catch ( ClassNotFoundException e) {e.printstacktrace ();}}}
Test results
Deep copy of Object