深克隆的实现方式

1. 所有对象都实现克隆方法

2. 通过构造方法实现深克隆

3. 使用JDK自带的字节流实现深克隆

(1)所有对象都实现克隆方法,这种方式需要让所有的引用对象都实现克隆(Cloneable 接口)

点击查看代码
package com.clone;

public class CloneExample {

    public static void main(String[] args) throws CloneNotSupportedException {
        //创建被赋值对象
        Address address=new Address(001,"北京");
        People p1=new People(1,"Java",address);
        //克隆p1对象
        People p2 = p1.clone();
        //修改原型对象
        p2.getAddress().setCity("深圳");
        //输出p1和p2地址信息
        System.out.println("p1:" +p1.getAddress().getCity()+"p2:"+p2.getAddress().getCity());

    }


    static class People implements Cloneable{
        private Integer id;
        private String name;
        private Address address;

        @Override
        protected People clone() throws CloneNotSupportedException {
            People people = (People) super.clone();
            people.setAddress(this.address.clone());
            //引用类型克隆赋值
            return people;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Address getAddress() {
            return address;
        }

        public void setAddress(Address address) {
            this.address = address;
        }

        public People(Integer id, String name, Address address) {
            this.id = id;
            this.name = name;
            this.address = address;
        }


    }

    static class Address implements Cloneable{
        private Integer id;
        private String city;

        public Address(Integer id, String city) {
            this.id = id;
            this.city = city;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

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

}


(2)通过构造方法实现深克隆

点击查看代码
package com.clone;

public class SecondExample {
    public static void main(String[] args) {
        //创建对象
        Address address=new Address(001,"北京");
        People p1=new People(1,"Java",address);
        //调用构造函数克隆对象
        //如果构造器的参数为基本数据类型或字符串类型则直接赋值,如果是对象类型,则需要重新 new 一个对象
        People p2 = new People(p1.getId(), p1.getName(), new Address(p1.getAddress().getId(), p1.getAddress().getCity()));
        //修改原型对象
        p1.getAddress().setCity("广州");
        //输出p1和p2地址信息
        System.out.println("p1:"+p1.getAddress().getCity()+"p2:"+p2.getAddress().getCity());
    }

    static class People {
        private Integer id;
        private String name;
        private Address address;


        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Address getAddress() {
            return address;
        }

        public void setAddress(Address address) {
            this.address = address;
        }

        public People(Integer id, String name, Address address) {
            this.id = id;
            this.name = name;
            this.address = address;
        }


    }
    static class Address {
        private Integer id;
        private String city;

        public Address(Integer id, String city) {
            this.id = id;
            this.city = city;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

    }
}

(3)使用JDK自带的字节流实现深克隆

点击查看代码
package com.clone;

import java.io.*;

public class ThirdExample {
    public static void main(String[] args) {
        //创建对象
        Address address = new Address(001, "北京");
        People p1 = new People(1, "java", address);
        //通过字节流实现克隆
        People p2 = (People) StreamClone.clone(p1);
        //修改原型对象
        p1.getAddress().setCity("上海");
        //输出p1和p2地址信息
        System.out.println("p1:"+p1.getAddress().getCity()+"p2:"+p2.getAddress().getCity());


    }
    static class StreamClone{
        public static <T extends Serializable> T clone(People obj){
            T cloneObj=null;
            try {
                ByteArrayOutputStream bo = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(bo);
                oos.writeObject(obj);
                oos.close();
                //分配内存,写入原始对象,生成新对象
                ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
                //获取上面的输出字节流
                ObjectInputStream oi = new ObjectInputStream(bi);
                //返回生成新的对象
                cloneObj=(T) oi.readObject();
                oi.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return cloneObj;

        }
    }

    static class People  implements Serializable {
        private Integer id;
        private String name;
        private Address address;



        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Address getAddress() {
            return address;
        }

        public void setAddress(Address address) {
            this.address = address;
        }

        public People(Integer id, String name, Address address) {
            this.id = id;
            this.name = name;
            this.address = address;
        }


    }
    static class Address implements Serializable{
        private Integer id;
        private String city;

        public Address(Integer id, String city) {
            this.id = id;
            this.city = city;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

    }
}

posted @   愚昧小生  阅读(56)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示