深克隆和浅克隆

1.克隆

在这里插入图片描述

克隆是指创建对象的一个副本,使副本具有与原始对象相同的属性和状态。
而克隆又分深克隆和浅克隆。

2.浅克隆

在这里插入图片描述

浅克隆:只会把原对象中类型为值的属性复制一份,然后引用数据类型就把它们在内存中的地址复制过去
比如一个对象有

 	public int age;
    public String name;
    public int[] arr;
    public Address address;

这时候浅拷贝出来了一个对象,改变这个克隆对象的age和name之后,当通过set方法改变了克隆对象的age和name,克隆对象的age和name和原对象就不同
如果试图改变arr或者address的话,那么就原对象也会跟着改变,因为引用对象拷贝的只是地址
下面看代码示例

class Address{
    public String city;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}
class Human implements Cloneable{
    public int age;
    public String name;
    public Address address;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    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 Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class Test15 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Address address=new Address();
        address.setCity("北京");
        Human human=new Human();
        human.setAge(10);
        human.setName("小白");
        human.setAddress(address);
        System.out.println("克隆前:");
        System.out.println(human.getName());
        System.out.println(human.getAge());
        System.out.println(human.getAddress().getCity());


        System.out.println("===============================================");


        Human humanClone= (Human) human.clone();
        humanClone.setName("大白");
        humanClone.setAge(20);
        address.setCity("上海");
        humanClone.setAddress(address);
        System.out.println("克隆后:");
        System.out.println(humanClone.getName());
        System.out.println(humanClone.getAge());
        System.out.println(humanClone.getAddress().getCity());
        //下面这行代码的结果你会发现原对象的city也变成上海了
        System.out.println(human.getAddress().getCity());


        System.out.println("===============================================");
        System.out.println("克隆后的引用类型对比");
        System.out.println("原对象:"+human.getAddress().getCity());
        System.out.println("拷贝对象:"+humanClone.getAddress().getCity());

        System.out.println("===============================================");
        System.out.println("克隆后的基本数据类型对比");
        System.out.println("原对象:"+human.getName());
        System.out.println("原对象:"+human.getAge());
        System.out.println("拷贝对象:"+humanClone.getName());
        System.out.println("拷贝对象:"+humanClone.getAge());

    }
}

输出结果是
在这里插入图片描述

3.深克隆

深克隆:将原对象的所有类型,都拷贝一份。
在这里插入图片描述
深克隆的话,如果你修改arr或者address的话,原对象不会跟着改变了
下面看代码

class Address implements Cloneable{
    public String city;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Human implements Cloneable{
    public int age;
    public String name;
    public Address address;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    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 Object clone() throws CloneNotSupportedException {
        Human newClone= (Human) super.clone();
        newClone.address= (Address) address.clone();
        return newClone;
    }
}
public class Test15 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Address address=new Address();
        address.setCity("北京");
        Human human=new Human();
        human.setAge(10);
        human.setName("小白");
        human.setAddress(address);
        System.out.println("克隆前:");
        System.out.println(human.getName());
        System.out.println(human.getAge());
        System.out.println(human.getAddress().getCity());


        System.out.println("===============================================");


        Human humanClone= (Human) human.clone();
        humanClone.setName("大白");
        humanClone.setAge(20);
        Address addressClone= (Address) address.clone();
        addressClone.setCity("上海");
        humanClone.setAddress(addressClone);
        System.out.println("克隆后:");
        System.out.println(humanClone.getName());
        System.out.println(humanClone.getAge());
        System.out.println(humanClone.getAddress().getCity());

        System.out.println("===============================================");
        System.out.println("克隆后的引用类型对比");
        System.out.println("原对象:"+human.getAddress().getCity());
        System.out.println("拷贝对象:"+humanClone.getAddress().getCity());

        System.out.println("===============================================");
        System.out.println("克隆后的基本数据类型对比");
        System.out.println("原对象:"+human.getName());
        System.out.println("原对象:"+human.getAge());
        System.out.println("拷贝对象:"+humanClone.getName());
        System.out.println("拷贝对象:"+humanClone.getAge());

    }
}

运行结果只有下图红色圈圈改变了
在这里插入图片描述
代码的修改为下面几个图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值