JAVA 對象拷貝

1.java裏的clone分爲: 
A:淺複製(淺克隆): 淺複製僅僅複製所考慮的對象,而不復制它所引用的對象。 
b:深複製(深克隆):深複製把要複製的對象所引用的對象都複製了一遍。 
Java中對象的克隆,爲了獲取對象的一份拷貝,我們可以利用Object類的clone()方法。必須要遵循下面三點 
1.在派生類中覆蓋基類的clone()方法,並聲明爲public【Object類中的clone()方法爲protected的】。 
2.在派生類的clone()方法中,調用super.clone()。 
3.在派生類中實現Cloneable接口。 

Object類裏的clone方法是淺複製(淺克隆) 

淺複製(淺克隆)的例子如下: 

  1. public class CloneTest  
  2. {  
  3.   
  4.     public static void main(String[] args) throws Exception  
  5.     {  
  6.         // teacher對象將被clone出來的Student對象共享.  
  7.         Teacher teacher = new Teacher();  
  8.         teacher.setAge(40);  
  9.         teacher.setName("Teacher zhang");  
  10.   
  11.         Student student1 = new Student();  
  12.         student1.setAge(20);  
  13.         student1.setName("zhangsan");  
  14.         student1.setTeacher(teacher);  
  15.   
  16.         // 複製出來一個對象student2  
  17.         Student student2 = (Student) student1.clone();  
  18.         System.out.println(student2.getAge());  
  19.         System.out.println(student2.getName());  
  20.   
  21.         System.out.println("~~~~~~~~~~~~~~~~~~~~~~");  
  22.         System.out.println(student1.getTeacher().getAge());  
  23.         System.out.println(student1.getTeacher().getName());  
  24.   
  25.         // 修改student2的引用對象  
  26.         student2.getTeacher().setAge(50);  
  27.         student2.getTeacher().setName("Teacher Li");  
  28.   
  29.         System.out.println("~~~~~~~~~~~~~~~~~~~~~~");  
  30.         System.out.println(student1.getTeacher().getAge());  
  31.         System.out.println(student1.getTeacher().getName());  
  32.     }  
  33. }  
  34.   
  35. class Teacher  
  36. {  
  37.     public int age;  
  38.     public String name;  
  39.   
  40.     public int getAge()  
  41.     {  
  42.         return age;  
  43.     }  
  44.   
  45.     public void setAge(int age)  
  46.     {  
  47.         this.age = age;  
  48.     }  
  49.   
  50.     public String getName()  
  51.     {  
  52.         return name;  
  53.     }  
  54.   
  55.     public void setName(String name)  
  56.     {  
  57.         this.name = name;  
  58.     }  
  59.   
  60. }  
  61.   
  62. class Student implements Cloneable  
  63. {  
  64.   
  65.     public int age;  
  66.     public String name;  
  67.     public Teacher teacher;  
  68.   
  69.     public int getAge()  
  70.     {  
  71.         return age;  
  72.     }  
  73.   
  74.     public void setAge(int age)  
  75.     {  
  76.         this.age = age;  
  77.     }  
  78.   
  79.     public String getName()  
  80.     {  
  81.         return name;  
  82.     }  
  83.   
  84.     public void setName(String name)  
  85.     {  
  86.         this.name = name;  
  87.     }  
  88.   
  89.     public Teacher getTeacher()  
  90.     {  
  91.         return teacher;  
  92.     }  
  93.   
  94.     public void setTeacher(Teacher teacher)  
  95.     {  
  96.         this.teacher = teacher;  
  97.     }  
  98.   
  99.     @Override  
  100.     public Object clone() throws CloneNotSupportedException  
  101.     {  
  102.         return super.clone();  
  103.     }  
  104. }  
  105. 輸出結果爲:  
  106. 20  
  107. zhangsan  
  108. ~~~~~~~~~~~~~~~~~~~~~~  
  109. 40  
  110. Teacher zhang  
  111. ~~~~~~~~~~~~~~~~~~~~~~  
  112. 50  
  113. Teacher Li  


2.深複製(深Clone)例子:

  1. public class DeepCloneTest  
  2. {  
  3.   
  4.     public static void main(String[] args) throws Exception  
  5.     {  
  6.         // teacher對象將不被clone出來的Student對象共享.  
  7.         Teacher teacher = new Teacher();  
  8.         teacher.setAge(40);  
  9.         teacher.setName("Teacher zhang");  
  10.   
  11.         Student student1 = new Student();  
  12.         student1.setAge(20);  
  13.         student1.setName("zhangsan");  
  14.         student1.setTeacher(teacher);  
  15.   
  16.         // 複製出來一個對象student2  
  17.         Student student2 = (Student) student1.clone();  
  18.         System.out.println(student2.getAge());  
  19.         System.out.println(student2.getName());  
  20.   
  21.         System.out.println("~~~~~~~~~~~~~~~~~~~~~~");  
  22.         System.out.println(student1.getTeacher().getAge());  
  23.         System.out.println(student1.getTeacher().getName());  
  24.   
  25.         // 修改student2的引用對象  
  26.         student2.getTeacher().setAge(50);  
  27.         student2.getTeacher().setName("Teacher Li");  
  28.   
  29.         System.out.println("~~~~~~~~~~~~~~~~~~~~~~");  
  30.         System.out.println(student1.getTeacher().getAge());  
  31.         System.out.println(student1.getTeacher().getName());  
  32.     }  
  33. }  
  34.   
  35. class Teacher implements Cloneable  
  36. {  
  37.     public int age;  
  38.     public String name;  
  39.   
  40.     public int getAge()  
  41.     {  
  42.         return age;  
  43.     }  
  44.   
  45.     public void setAge(int age)  
  46.     {  
  47.         this.age = age;  
  48.     }  
  49.   
  50.     public String getName()  
  51.     {  
  52.         return name;  
  53.     }  
  54.   
  55.     public void setName(String name)  
  56.     {  
  57.         this.name = name;  
  58.     }  
  59.   
  60.     @Override  
  61.     public Object clone() throws CloneNotSupportedException  
  62.     {  
  63.         return super.clone();  
  64.     }  
  65.   
  66. }  
  67.   
  68. class Student implements Cloneable  
  69. {  
  70.   
  71.     public int age;  
  72.     public String name;  
  73.     public Teacher teacher;  
  74.   
  75.     public int getAge()  
  76.     {  
  77.         return age;  
  78.     }  
  79.   
  80.     public void setAge(int age)  
  81.     {  
  82.         this.age = age;  
  83.     }  
  84.   
  85.     public String getName()  
  86.     {  
  87.         return name;  
  88.     }  
  89.   
  90.     public void setName(String name)  
  91.     {  
  92.         this.name = name;  
  93.     }  
  94.   
  95.     public Teacher getTeacher()  
  96.     {  
  97.         return teacher;  
  98.     }  
  99.   
  100.     public void setTeacher(Teacher teacher)  
  101.     {  
  102.         this.teacher = teacher;  
  103.     }  
  104.   
  105.     @Override  
  106.     public Object clone() throws CloneNotSupportedException  
  107.     {  
  108.         Student student = (Student) super.clone();  
  109.         // 將引用的對象teacher也clone下  
  110.         student.setTeacher((Teacher) (student.getTeacher().clone()));  
  111.         return student;  
  112.     }  
  113. }  
  114.   
  115. 輸出結果爲:  
  116. 20  
  117. zhangsan  
  118. ~~~~~~~~~~~~~~~~~~~~~~  
  119. 40  
  120. Teacher zhang  
  121. ~~~~~~~~~~~~~~~~~~~~~~  
  122. 40  
  123. Teacher zhang  

3.利用序列化來做深複製,把對象寫到流裏的過程是序列化(Serilization)過程,而把對象從流中讀出來的過程則叫做反序列化(Deserialization)過程。應當指出的是,寫在流裏的是對象的一個拷貝,而原對象仍然存在於JVM裏面。,利用這個特性,可以做深拷貝 。

  1. import java.io.ByteArrayInputStream;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.ObjectInputStream;  
  4. import java.io.ObjectOutputStream;  
  5. import java.io.Serializable;  
  6. //利用序列化來做深複製  
  7. //深clone  
  8.   
  9. public class DeepCloneTest  
  10. {  
  11.   
  12.     public static void main(String[] args) throws Exception  
  13.     {  
  14.         // teacher對象將不被clone出來的Student對象共享.  
  15.         Teacher teacher = new Teacher();  
  16.         teacher.setAge(40);  
  17.         teacher.setName("Teacher zhang");  
  18.   
  19.         Student student1 = new Student();  
  20.         student1.setAge(20);  
  21.         student1.setName("zhangsan");  
  22.         student1.setTeacher(teacher);  
  23.   
  24.         // 複製出來一個對象student2  
  25.         Student student2 = (Student) student1.deepCopy();  
  26.         System.out.println(student2.getAge());  
  27.         System.out.println(student2.getName());  
  28.   
  29.         System.out.println("~~~~~~~~~~~~~~~~~~~~~~");  
  30.         System.out.println(student1.getTeacher().getAge());  
  31.         System.out.println(student1.getTeacher().getName());  
  32.   
  33.         // 修改student2的引用對象  
  34.         student2.getTeacher().setAge(50);  
  35.         student2.getTeacher().setName("Teacher Li");  
  36.   
  37.         System.out.println("~~~~~~~~~~~~~~~~~~~~~~");  
  38.         System.out.println(student1.getTeacher().getAge());  
  39.         System.out.println(student1.getTeacher().getName());  
  40.     }  
  41. }  
  42.   
  43. class Teacher implements Serializable  
  44. {  
  45.   
  46.     private static final long serialVersionUID = -8834559347461591191L;  
  47.   
  48.     public int age;  
  49.     public String name;  
  50.   
  51.     public int getAge()  
  52.     {  
  53.         return age;  
  54.     }  
  55.   
  56.     public void setAge(int age)  
  57.     {  
  58.         this.age = age;  
  59.     }  
  60.   
  61.     public String getName()  
  62.     {  
  63.         return name;  
  64.     }  
  65.   
  66.     public void setName(String name)  
  67.     {  
  68.         this.name = name;  
  69.     }  
  70.   
  71. }  
  72.   
  73. class Student implements Serializable  
  74. {  
  75.   
  76.     // serialVersionUID  
  77.     // 如果你的對象序列化後存到硬盤上面後,可是後來你卻更改了類的field(增加或減少或改名),當你反序列化時,就會出現Exception的,這樣就會造成不兼容性的問題。  
  78.     // 但當serialVersionUID相同時,它就會將不一樣的field以type的缺省值賦值(如int型的是0,String型的是null等),這個可以避開不兼容性的問題。所以最好給serialVersionUID賦值  
  79.     private static final long serialVersionUID = 7991552226614088458L;  
  80.   
  81.     public int age;  
  82.     public String name;  
  83.     public Teacher teacher;  
  84.   
  85.     public int getAge()  
  86.     {  
  87.         return age;  
  88.     }  
  89.   
  90.     public void setAge(int age)  
  91.     {  
  92.         this.age = age;  
  93.     }  
  94.   
  95.     public String getName()  
  96.     {  
  97.         return name;  
  98.     }  
  99.   
  100.     public void setName(String name)  
  101.     {  
  102.         this.name = name;  
  103.     }  
  104.   
  105.     public Teacher getTeacher()  
  106.     {  
  107.         return teacher;  
  108.     }  
  109.   
  110.     public void setTeacher(Teacher teacher)  
  111.     {  
  112.         this.teacher = teacher;  
  113.     }  
  114.   
  115.     public Object deepCopy() throws Exception  
  116.     {  
  117.         // 將該對象序列化成流,因爲寫在流裏的是對象的一個拷貝,而原對象仍然存在於JVM裏面。所以利用這個特性可以實現對象的深拷貝  
  118.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  119.   
  120.         ObjectOutputStream oos = new ObjectOutputStream(bos);  
  121.   
  122.         oos.writeObject(this);  
  123.   
  124.         // 將流序列化成對象  
  125.         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());  
  126.   
  127.         ObjectInputStream ois = new ObjectInputStream(bis);  
  128.   
  129.         return ois.readObject();  
  130.     }  
  131. }  
  132.   
  133. 輸出結果爲:  
  134. 20  
  135. zhangsan  
  136. ~~~~~~~~~~~~~~~~~~~~~~  
  137. 40  
  138. Teacher zhang  
  139. ~~~~~~~~~~~~~~~~~~~~~~  
  140. 40  
  141. Teacher zhang 


原文:http://blog.csdn.net/jdluojing/article/details/6963112

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章