Java的两种深拷贝(原型模式)

Java 专栏收录该内容
25 篇文章 1 订阅

克隆

  • 实现Cloneable
  • 重写clone()方法
    @Override
    protected Object clone() throws CloneNotSupportedException {
        DeepPrototype deepPrototype = null;
        deepPrototype = (DeepPrototype) super.clone();
        deepPrototype.target = (DeepCloneableTarget) this.target.clone();

        return deepPrototype;
    }

序列化(推荐)

  • 实现Serializable
  • 定义serialVersionUID
    public DeepPrototype deepClone() {

        //创建流
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try{
            //将本类输出到内存中
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this);

            //从内存中读取
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            DeepPrototype o = (DeepPrototype) ois.readObject();
            return o;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (bos != null) bos.close();
                if (oos != null) oos.close();
                if (bis != null) bis.close();
                if (ois != null) ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
将序列化的类输出到本地文件
    public static void cloneFile() throws IOException, ClassNotFoundException {
        //序列化到本地
        FileOutputStream fos = new FileOutputStream("E:/ProgramData/Project/Object.out");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        DeepCloneableTarget target = new DeepCloneableTarget("李四", "张三");
        oos.writeObject(target);
        oos.flush();
        oos.close();
        fos.close();

        //读取本地序列化后的文件
        FileInputStream fis = new FileInputStream("E:/ProgramData/Project/Object.out");
        ObjectInputStream ois = new ObjectInputStream(fis);
        target = (DeepCloneableTarget) ois.readObject();
        ois.close();
        fis.close();
        System.out.println(target);
    }

整体代码

public class DeepCloneableTarget implements Serializable, Cloneable {

    private static final long serialVersionUID = 1L;

    private String cloneName;
    private String cloneClass;

    public DeepCloneableTarget(String cloneName, String cloneClass) {
        this.cloneName = cloneName;
        this.cloneClass = cloneClass;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "DeepCloneableTarget{" +
                "cloneName='" + cloneName + '\'' +
                ", cloneClass='" + cloneClass + '\'' +
                '}';
    }
}
public class DeepPrototype implements Serializable, Cloneable {

    private static final long serialVersionUID = -1897843806793850888L;

    public String name;
    public DeepCloneableTarget target;

    public DeepPrototype() {
    }

    /**
     * 深拷贝  clone(),序列化
     *  方式一
     * @return
     * @throws CloneNotSupportedException
     */
    @Override
    protected Object clone() throws CloneNotSupportedException {
        DeepPrototype deepPrototype = null;
        deepPrototype = (DeepPrototype) super.clone();
        deepPrototype.target = (DeepCloneableTarget) this.target.clone();

        return deepPrototype;
    }

    /**
     * 深拷贝
     *  方式二: 序列化
     */
    public DeepPrototype deepClone() {

        //创建流
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try{
            //将本类输出到内存中
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this);

            //从内存中读取
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            DeepPrototype o = (DeepPrototype) ois.readObject();
            return o;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (bos != null) bos.close();
                if (oos != null) oos.close();
                if (bis != null) bis.close();
                if (ois != null) ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 0
    点赞
  • 4
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值