`

利用序列化实现java深度拷贝

阅读更多
对象深度拷贝
利用序列化与反序列化对对象进行深度复制

Java代码

   1. public class Student implements Serializable { 
   2.  
   3.     private String name; 
   4.     private String tel; 
   5.     private int age; 
   6.      
   7.     //set and get..... 
   8. } 
   9.  
  10. public class Teacher implements Serializable { 
  11.  
  12.     private String name; 
  13.     private int age; 
  14.     private List<Student> stuList; 
  15.  
  16.     //set and get.... 
  17.  
  18. } 
  19.  
  20. import java.io.ByteArrayInputStream; 
  21. import java.io.ByteArrayOutputStream; 
  22. import java.io.IOException; 
  23. import java.io.ObjectInputStream; 
  24. import java.io.ObjectOutputStream; 
  25. import java.util.ArrayList; 
  26. import java.util.List; 
  27.  
  28. public class Test { 
  29.  
  30.      
  31.     public static void main(String[] args) { 
  32.         Student s1 = new Student(); 
  33.         s1.setName("Wang"); 
  34.         s1.setAge(25); 
  35.         s1.setTel("110"); 
  36.         Student s2 = new Student(); 
  37.         s2.setName("Li"); 
  38.         s2.setAge(35); 
  39.         s2.setTel("119"); 
  40.          
  41.          
  42.         Teacher t = new Teacher(); 
  43.         t.setName("Zhang"); 
  44.         t.setAge(50); 
  45.         List<Student> stuList = new ArrayList<Student>(); 
  46.         stuList.add(s1); 
  47.         stuList.add(s2); 
  48.         t.setStuList(stuList);       
  49.         System.out.println("t: "+t); 
  50.         System.out.println("s1: "+s1); 
  51.         System.out.println("s2: "+s2); 
  52.         System.out.println("after clone--------------"); 
  53.         Teacher t2 = (Teacher) depthClone(t); 
  54.         System.out.println("t2: "+t2); 
  55.         System.out.println("t: "+t); 
  56.          
  57.         Student s3 = t2.getStuList().get(0); 
  58.         Student s4 = t2.getStuList().get(1); 
  59.         System.out.println("s3: "+s3); 
  60.         System.out.println("s1: "+s1); 
  61.         System.out.println("s4: "+s4); 
  62.         System.out.println("s2: "+s2); 
  63.          
  64.         System.out.println("after change--------------"); 
  65.         t2.setAge(55); 
  66.         t2.setName("Zhao"); 
  67.          
  68.         System.out.println("t2 Name: "+t2.getName()+" t2 age"+t2.getAge()); 
  69.         System.out.println("t Name: "+t.getName()+" t age"+t.getAge()); 
  70.  
  71.     } 
  72.      
  73.     private static Object depthClone(Object srcObj){ 
  74.         Object cloneObj = null; 
  75.         try { 
  76.             ByteArrayOutputStream out = new ByteArrayOutputStream(); 
  77.             ObjectOutputStream oo = new ObjectOutputStream(out); 
  78.             oo.writeObject(srcObj); 
  79.              
  80.             ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 
  81.             ObjectInputStream oi = new ObjectInputStream(in); 
  82.             cloneObj = oi.readObject();          
  83.         } catch (IOException e) { 
  84.             e.printStackTrace(); 
  85.         } catch (ClassNotFoundException e) { 
  86.             e.printStackTrace(); 
  87.         } 
  88.         return cloneObj; 
  89.     } 
  90.  
  91. } 

public class Student implements Serializable {

private String name;
private String tel;
private int age;

//set and get.....
}

public class Teacher implements Serializable {

private String name;
private int age;
private List<Student> stuList;

//set and get....

}

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class Test {


public static void main(String[] args) {
Student s1 = new Student();
s1.setName("Wang");
s1.setAge(25);
s1.setTel("110");
Student s2 = new Student();
s2.setName("Li");
s2.setAge(35);
s2.setTel("119");


Teacher t = new Teacher();
t.setName("Zhang");
t.setAge(50);
List<Student> stuList = new ArrayList<Student>();
stuList.add(s1);
stuList.add(s2);
t.setStuList(stuList);
System.out.println("t: "+t);
System.out.println("s1: "+s1);
System.out.println("s2: "+s2);
System.out.println("after clone--------------");
Teacher t2 = (Teacher) depthClone(t);
System.out.println("t2: "+t2);
System.out.println("t: "+t);

Student s3 = t2.getStuList().get(0);
Student s4 = t2.getStuList().get(1);
System.out.println("s3: "+s3);
System.out.println("s1: "+s1);
System.out.println("s4: "+s4);
System.out.println("s2: "+s2);

System.out.println("after change--------------");
t2.setAge(55);
t2.setName("Zhao");

System.out.println("t2 Name: "+t2.getName()+" t2 age"+t2.getAge());
System.out.println("t Name: "+t.getName()+" t age"+t.getAge());

}

private static Object depthClone(Object srcObj){
Object cloneObj = null;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(out);
oo.writeObject(srcObj);

ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream oi = new ObjectInputStream(in);
cloneObj = oi.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return cloneObj;
}

}



输入结果:
t: jame.jsp.bean.Teacher@affc70
s1: jame.jsp.bean.Student@1e63e3d
s2: jame.jsp.bean.Student@1004901
after clone--------------
t2: jame.jsp.bean.Teacher@1f14ceb
t: jame.jsp.bean.Teacher@affc70
s3: jame.jsp.bean.Student@f0eed6
s1: jame.jsp.bean.Student@1e63e3d
s4: jame.jsp.bean.Student@1d05c81
s2: jame.jsp.bean.Student@1004901
after change--------------
t2 Name: Zhao t2 age55
t Name: Zhang t age50

*采用些方法做深度拷贝时,要求所有对象implements Serializable,否则报java.io.NotSerializableException异常。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics