Java 深拷贝(深复制或深克隆)与浅拷贝(浅复制或浅克隆)

本文详细介绍了Java中的深拷贝和浅拷贝概念,通过实例展示了如何实现这两种拷贝方式。在浅拷贝中,复制的对象引用了原对象的内部对象,导致修改一处会影响到另一处。而在深拷贝中,不仅复制对象本身,还复制了其引用的对象,确保了对象的独立性。文章最后讨论了解决浅拷贝问题的深拷贝实现,包括手动复制属性和使用序列化与反序列化的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java 深拷贝(深复制或深克隆)和浅拷贝(浅复制或浅克隆)

浅复制:被复制对象的所有变量都含有与原来对象相同的值,而所有其他对象的引用仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。

深复制:被复制对象的所有变量都含有与原来对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制的新对象,而不再是原有的那些被引用的对象。换言之,深复制把复制的对象所引用的对象都复制了一遍。

克隆的实现方式:

1. 类要实现Clonable接口

2. 重写Object的clone方法

3. 对象调用clone方法

浅复制

案例(编写大象类)

//实现Cloneable接口
public class Elephant implements Serializable,Cloneable {
    private String name;
    private String age;
    private String sex;
    private Country country =new Country();
	
	//重写Object的clone方法
	  @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public Elephant(String name, String age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "Elephant{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    }

测试

public class CloneTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        Elephant elephant = new Elephant("小红象", "20", "男");
        //克隆大象
        Elephant elephant1 = (Elephant) elephant.clone();
        System.out.println(elephant);
         System.out.println(elephant1);
        System.out.println(elephant==elephant1);
    }
}

在这里插入图片描述
  但是浅复制会存在一个问题,当我们给其中一头大象修改地区时,另一个大象地区也会同时进行改变,而大象的基本数据不会改变

添加地区类

public class Country implements Serializable {
    private String name="亚洲";

    public String getName() {
        return name;
    }

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

测试类

public class CloneTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        Elephant elephant = new Elephant("小红象", "20", "男");
        //克隆大象
        Elephant elephant1 = (Elephant) elephant.clone();
        System.out.println(elephant);
        System.out.println(elephant1);
        System.out.println(elephant==elephant1);
		//将原来的大象的年龄改为10岁
        elephant.setAge("10");
        //将克隆出来的大象的地区改为干果
        elephant1.getCountry().setName("干果");
        System.out.println(elephant+elephant.getCountry().getName());
        System.out.println(elephant1+elephant1.getCountry().getName());
        System.out.println(elephant==elephant1);
    }
}

运行结果

在这里插入图片描述
总结:基本数据类型和String完全赋值的值,引用自定义类型赋值的是内存地址
在这里插入图片描述
解决上述问题就需要采用深复制

深复制

1.引用类型的属性一个个创建,然后赋值

//实现Cloneable接口
public class Elephant implements Serializable,Cloneable {
    private String name;
    private String age;
    private String sex;
    private Country country =new Country();
    
  @Override
    protected Object clone() throws CloneNotSupportedException {
        Elephant clone = (Elephant) super.clone();
        Country country = new Country();
        country.setName(this.country.getName());
        clone.setCountry(country);
        return clone;
    }

    public Elephant(String name, String age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "Elephant{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

运行结果

在这里插入图片描述
属性一个个进行创建和赋值代码比较繁琐,工作量也十分的大,所有一般采用序列化和反序列化

2.序列化和反序列化

在这里插入图片描述

序列化和反序列化请点击次链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值