Member variables within the object has more than one object type to realize how deep clone

Deep clone implemented in two ways:

1) based on shallow clone of the by -implemented method coverage clone Object class ().

In order to achieve true copy an object, rather than purely a reference copy. We need to replicate class of Address, and modifying the clone method, complete code is as follows:

Copy the code
 1 package abc;  
 2   
 3 class Address implements Cloneable {  
 4     private String add;  
 5   
 6     public String getAdd() {  
 7         return add;  
 8     }  
 9   
10     public void setAdd(String add) {  
11         this.add = add;  
12     }  
13       
14     @Override  
15     public Object clone() {  
16         Address addr = null;  
17         try{  
18             addr = (Address)super.clone();  
19         }catch(CloneNotSupportedException e) {  
20             e.printStackTrace();  
21         }  
22         return addr;  
23     }  
24 }  
25   
26 class Student implements Cloneable{  
27     private int number;  
28   
29     private Address addr;  
30       
31     public Address getAddr() {  
32         return addr;  
33     }  
34   
35     public void setAddr(Address addr) {  
36         this.addr = addr;  
37     }  
38   
39     public int getNumber() {  
40         return number;  
41     }  
42   
43     public void setNumber(int number) {  
44         this.number = number;  
45     }  
46       
47     @Override  
48     public Object clone() {  
49         Student stu = null;  
50         try{  
51             stu = (Student)super.clone();   //浅复制  
52         }catch(CloneNotSupportedException e) {  
53             e.printStackTrace();  
54         }  
55         stu.addr = (Address)addr.clone();   //深度复制  
56         return stu;  
57     }  
58 }  
59 public class Test {  
60       
61     public static void main(String args[]) {  
62           
63         Address addr = new Address();  
64         addr.setAdd("杭州市");  
65         Student stu1 = new Student();  
66         stu1.setNumber(123);  
67         stu1.setAddr(addr);  
68           
69         Student stu2 = (Student)stu1.clone();  
70            
71 is System.out.println ( "Student 1:" + stu1.getNumber () + ", Address:." + Stu1.getAddr () getAdd ());   
72 System.out.println ( "Student 2:" + stu2.getNumber () + ", address:" + stu2.getAddr () getAdd ());.   
73 is            
74 addr.setAdd ( "West Lake District");   
75            
76 System.out.println ( "student 1:" + stu1 .getNumber () + ", address:" + stu1.getAddr () getAdd ());.   
77 System.out.println ( "student 2:" + stu2.getNumber () + ", address:" + stu2.getAddr () .getAdd ());   
78}   
79}  
Copy the code

result:

Student 1: 123, Address: Hangzhou   
Student 2: 123, Address: Hangzhou   
Student 1: 123, Address: West Lake District   
Student 2: 123, Address: Hangzhou  

2) by a sequence of (the Serialization) manner (clone multilayer solve the problem).

If the reference type which also contains many references to the type, or class of which the inner reference type contains a reference to another type, the method using the clone will be very cumbersome. Then we can use the serialization approach to achieve deep cloning objects.

Serialization is the process flow of objects written, to the stream object is a copy of the original object, and the original object still exists in memory. Serialization achieved by copying only the object itself can be copied, and its member object references can be copied, so written by a sequence of object streams, and from which the stream read out a deep clone may be achieved. Note that to achieve its serialized object class must implement the Serializable interface, or can not achieve serialization operation.

Code Serializable Cloneable interface and interfaces Java language provides a very simple, they are empty interface, this interface is also known as an empty marker interface that identifies the interface does not define any method of, its role is to tell the JRE implementation class whether these interfaces having a function, such as whether to support cloning, sequence and so support

Outer the implements the Serializable class public. 1 { 
 2 Private Long serialVersionUID = Final static 369285298572941L; 
 . 3 public Inner Inner; 
 . 4 // Discription: [deep copy method, a subject in need, and all objects are achieved serialized object properties] 
 . 5 Outer public myclone () { 
 . 6 Outer Outer = null; 
 . 7 the try {  // 将该对象序列化成流,因为写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面。所以利用这个特性可以实现对象的深拷贝
 . 8 ByteArrayOutputStream new new ByteArrayOutputStream BAOS = (); 
 . 9 the ObjectOutputStream the ObjectOutputStream OOS new new = (BAOS); 
10 oos.writeObject (the this); 
. 11 // stream sequence into the target 
12 ByteArrayInputStream bais = new A ByteArrayInputStream (baos.toByteArray ()); 
13 is the ObjectInputStream new new OIS = the ObjectInputStream (Bais); 
14 = Outer (Outer) ois.readObject (); 
15} the catch (IOException E) {
16           e.printStackTrace();
17       } catch (ClassNotFoundException e) {
18           e.printStackTrace();
19       }
20       return outer;
21   }
22 }
Copy the code
Copy the code
  */
 1 public class Inner implements Serializable{
 2   private static final long serialVersionUID = 872390113109L;
 3   public String name = "";
 4 
 5   public Inner(String name) {
 6       this.name = name;
 7   }
 8 
 9   @Override
10   public String toString() {
11       return "Inner的name值为:" + name;
12   }
13 }
Copy the code

This also enables the presence of two completely independent objects within the memory space, independently of each other's values.

to sum up

There are two ways to achieve the object cloning:

  1) to achieve Cloneable interface and override clone () method of class Object;

  2) implement Serializable achieved by cloning serialization and deserialization of the objects, the real depth of the clones can be achieved.

                             note

Based on cloning serialization and de-serialization to achieve not only the depth of cloning, more importantly, is limited by generics, you can check out if you want to clone objects support serialization, this check is complete compiler, not at runtime throwing an exception, this scheme is much better than the method using the clone clone Object class. Let problems at compile time exposed always better to leave the matter to the runtime.

Guess you like

Origin www.cnblogs.com/kise-ryota/p/11201479.html