利用序列化与反序列化对Java对象进行深度复制
来源 http://lhg803.iteye.com/blog/407270
JDK的API中的java.lang.Object和Cloneable接口要完美实现深度clone时,都需要针对具体的类改代码。
采用序列化与反序列化可以完全独立于具体的某个类进行深度复制。
采用此方法做深度拷贝时,要求所有对象implements Serializable,否则报java.io.NotSerializableException异常。
实例:
学生类
package cup1;
import java.io.Serializable;
public class Student implements Serializable{
private String name;
private String tel;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
教师类,包括一个学生对象列表
package cup1;
import java.io.Serializable;
import java.util.List;
public class Teacher implements Serializable{
private String name;
private int age;
private List<Student> stuList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Student> getStuList() {
return stuList;
}
public void setStuList(List<Student> stuList) {
this.stuList = stuList;
}
}
测试类,包括main方法和deepClone对象深度克隆方法
package cup1;
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("-------original-------");
System.out.println("t: " + t);
System.out.println("s1: " + s1);
System.out.println("s2: " + s2);
System.out.println("-------after clone-------");
Teacher t2 = (Teacher) deepClone(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 t2 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());
}
public static Object deepClone(Object src) {
Object dst = null;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(out);
oo.writeObject(src);
ByteArrayInputStream in = new ByteArrayInputStream(
out.toByteArray());
ObjectInputStream oi = new ObjectInputStream(in);
dst = oi.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return dst;
}
}
输出结果
-------original-------
t: cup1.Teacher@1fb8ee3
s1: cup1.Student@61de33
s2: cup1.Student@14318bb
-------after clone-------
t2: cup1.Teacher@12ac982
t: cup1.Teacher@1fb8ee3
s3: cup1.Student@1389e4
s1: cup1.Student@61de33
s4: cup1.Student@c20e24
s2: cup1.Student@14318bb
-------after t2 change-------
t2 Name: Zhao t2 age55
t Name: Zhang t age50