Java对象深度克隆

来源:互联网 发布:网络基础架构 编辑:程序博客网 时间:2024/04/30 01:07

对象的深度克隆原理:将对象序列化后写在输出流里,因为写在流里面的对象是一份拷贝,原对象仍然在JVM里;然后再把输出流转换为输入流,把对象反序列化后写出来!这样就实现了对象的深度克隆,克隆后的两个对象完全独立开来,互不影响!

你会发现对象的深度克隆其实是利用的对象的序列化和反序列化,所以要进行深度克隆的对象都要实现Serializable接口!

package com.test;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.ArrayList;import java.util.List;public class StudentClone implements Cloneable,Serializable    {         String name;         int age;              List<String> list = new ArrayList<String>(){{     add("A");     add("B");     add("C");     }};              StudentClone(String name,int age)         {            this.name=name;            this.age=age;         }              /*public Object clone()         {             Object o=null;             try             {                o=(StudentClone)super.clone();//Object 中的clone()识别出你要复制的是哪一个对象。             }             catch(CloneNotSupportedException e)             {                 System.out.println(e.toString());             }             return o;                   }  */           /*public StudentClone clone() {            ByteArrayOutputStream byteOut = null;            ObjectOutputStream objOut = null;            ByteArrayInputStream byteIn = null;            ObjectInputStream objIn = null;                        try {                byteOut = new ByteArrayOutputStream();                 objOut = new ObjectOutputStream(byteOut);                 objOut.writeObject(prototype);                   byteIn = new ByteArrayInputStream(byteOut.toByteArray());                objIn = new ObjectInputStream(byteIn);                                return (ContretePrototype) objIn.readObject();            } catch (IOException e) {                throw new RuntimeException("Clone Object failed in IO.",e);               } catch (ClassNotFoundException e) {                throw new RuntimeException("Class not found.",e);               } finally{                try{                    byteIn = null;                    byteOut = null;                    if(objOut != null) objOut.close();                       if(objIn != null) objIn.close();                   }catch(IOException e){                   }               }        }*/                       public Object copy() throws IOException, ClassNotFoundException{       ByteArrayOutputStream bos = new ByteArrayOutputStream();       ObjectOutputStream oos = new ObjectOutputStream(bos);       oos.writeObject(this);       ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));       return ois.readObject();      }     public static void main(String[] args) throws IOException, ClassNotFoundException    {    StudentClone s1= new StudentClone("zhangsan",50);  StudentClone s2=(StudentClone)s1.copy(); s2.list.add("D");    s2.name="lisi";        s2.age=20;        System.out.println("name="+s1.name+","+"age="+s1.age);//修改学生2后,不影响学生1的值。       System.out.println("name="+s2.name+","+"age="+s2.age);    System.out.println("name="+s1.name+","+"age="+s1.age);        System.out.println(s1.list.size()+"hhhhhhh");    System.out.println(s2.list.size()+"uuuuuuu");}   }

程序运行的结果如下图:两个List中的对象个数不相同了,也就是对克隆后的对象进行更改不影响原对象的List了,即实现了深度克隆



0 0