java关于深复制示例

来源:互联网 发布:mac os山狮系统 编辑:程序博客网 时间:2024/06/05 20:28
package com.test.serializable;
import java.io.Serializable;
public class Student implements Serializable{
private String name;
private int age;
private Teacher teacher;
public Teacher getTeacher() {
  return teacher;
}
public void setTeacher(Teacher teacher) {
  this.teacher = teacher;
}
public Student(String name,int age,Teacher teacher)
{
  this.name=name;
  this.age=age;
  this.teacher=teacher;
}
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;
}
}
package com.test.serializable;
import java.io.Serializable;
public class Teacher implements Serializable{
private String name;
private int age;
public Teacher(String name,int age)
{
  this.name=name;
  this.age=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;
}
}
package com.test.serializable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* 深复制
* @author 陈双
*
*/
public class Test {
private Student student;
private int version;
    public Student getStudent() {
  return student;
}
public void setStudent(Student student) {
  this.student = student;
}
public Test(Student student,int version)
{
  this.student=student;
  this.version=version;
}
public static void main(String[] args)
    {
     Teacher teacher=new Teacher("xiaoming",45);
     Student stu=new Student("accp",20,teacher);
     Test test=new Test(stu,6);
     try {
   Test newTest=(Test)test.clone();//创建一个副本
   System.out.println(stu.getAge()==newTest.getStudent().getAge());
  } catch (CloneNotSupportedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    }
    /**
     * 深复制
     * @param instance 需要复制的对象
     * @return 返回复制后的实例
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Object deepCopy(Object instance) throws IOException, ClassNotFoundException
    {
     ByteArrayOutputStream fos=new ByteArrayOutputStream();//字节数组输出流
     ObjectOutputStream out=new ObjectOutputStream(fos);//对象输出流
     out.writeObject(instance);//把对象写入到字节数组输出流中
     out.close();
     //字节数组读入流
     ByteArrayInputStream fis=new ByteArrayInputStream(fos.toByteArray());//把字节数组输出流中的写入的字节放入字节数组读入流中
     ObjectInputStream in=new ObjectInputStream(fis);//对象读入流
        Object ret=in.readObject();//从字节数组读入流中读取并构建一个对象
        in.close();
        return ret;
    }
protected Object clone() throws CloneNotSupportedException {
  // TODO Auto-generated method stub
  Test test=null;
  try {
   test = new Test((Student)deepCopy(this.student),this.version);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return test;
}
}
原创粉丝点击