package com.ygl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class CloneTest3 {
public static void main(String[] args) throws Exception {
Teacher3 t=new Teacher3();
t.setAge(30);
t.setName("ll");
Student3 stu=new Student3();
stu.setAge(26);
stu.setName("ygl");
stu.setTeacher3(t);
Student3 s=(Student3)stu.deepCopy();
System.out.println(s.getName());
System.out.println("-------------------------");
System.out.println(s.getTeacher3().getName());
t.setName("hh");
System.out.println(s.getTeacher3().getName());
}
}
class Teacher3 implements Serializable{
private String name;
private int age;
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;
}
}
class Student3 implements Serializable{
private String name;
private int age;
private Teacher3 teacher3;
public Teacher3 getTeacher3() {
return teacher3;
}
public void setTeacher3(Teacher3 teacher3) {
this.teacher3 = teacher3;
}
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 Object deepCopy() throws Exception{
ByteArrayOutputStream bos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bos);
oos.writeObject(this);
ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois=new ObjectInputStream(bis);
return ois.readObject();
}
}