java中的深拷贝与浅拷贝

在 Java 中,对象的拷贝可以分为深拷贝和浅拷贝两种:

浅拷贝(Shallow Copy)

浅拷贝是指只复制对象本身,而不复制对象引用的对象。也就是说,新对象和原对象共享同一个引用对象。

实现浅拷贝的方法:
  1. 使用 clone() 方法:

    Java 中的 clone() 方法可以用来实现浅拷贝。但是,为了使对象支持 clone() 方法,该对象必须实现 Cloneable 接口,并且重写 clone() 方法。

    class Student implements Cloneable {
        private String name;
        private int age;
    
        // 构造方法和其他方法
    
        // 实现 clone() 方法
        @Override
        public Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }
    

    使用 clone() 方法进行拷贝:

    try {
        Student original = new Student("Alice", 20);
        Student copy = (Student) original.clone();
        // 此时 original 和 copy 是两个不同的对象,但是它们的 name 和 age 属性引用的是同一个对象
    
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }
    
  2. 使用复制构造函数(如果定义了):

    如果类中提供了复制构造函数,可以用来实现浅拷贝。

    class Student {
        private String name;
        private int age;
    
        // 复制构造函数
        public Student(Student other) {
            this.name = other.name;
            this.age = other.age;
        }
    }
    

    使用复制构造函数进行拷贝:

    Student original = new Student("Bob", 25);
    Student copy = new Student(original);
    // 此时 original 和 copy 是两个不同的对象,但是它们的 name 和 age 属性引用的是同一个对象
    

深拷贝(Deep Copy)

深拷贝是指不仅复制对象本身,还复制对象引用的所有对象,因此新对象和原对象引用的是不同的对象实例。

实现深拷贝的方法:
  1. 手动实现深拷贝:

    手动实现深拷贝需要遍历对象引用的所有对象,并为每个对象创建一个新的实例。

    class Student implements Cloneable {
        private String name;
        private int age;
        private Address address;
    
        // 构造方法和其他方法
    
        // 实现 clone() 方法
        @Override
        public Object clone() throws CloneNotSupportedException {
            // 浅拷贝
            Student cloned = (Student) super.clone();
            // 深拷贝 address 对象
            cloned.address = (Address) address.clone();
            return cloned;
        }
    }
    
    class Address implements Cloneable {
        private String city;
        private String street;
    
        // 构造方法和其他方法
    
        // 实现 clone() 方法
        @Override
        public Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }
    

    使用深拷贝:

    try {
        Student original = new Student("Alice", 20, new Address("New York", "123 Street"));
        Student copy = (Student) original.clone();
        // 此时 original 和 copy 是两个不同的对象,并且它们的 address 引用的是两个不同的 Address 对象实例
    
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }
    
  2. 使用序列化和反序列化实现深拷贝:

    利用 Java 的序列化机制,将对象写入一个流中,然后从流中读取出来,可以实现深拷贝。

    import java.io.*;
    
    class Student implements Serializable {
        private String name;
        private int age;
        private Address address;
    
        // 构造方法和其他方法
    
        // 实现深拷贝
        public Student deepCopy() {
            try {
                // 将对象写入流中
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(this);
    
                // 从流中读取对象
                ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
                ObjectInputStream ois = new ObjectInputStream(bais);
                return (Student) ois.readObject();
    
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
    
    class Address implements Serializable {
        private String city;
        private String street;
    
        // 构造方法和其他方法
    }
    

    使用序列化和反序列化实现深拷贝:

    Student original = new Student("Alice", 20, new Address("New York", "123 Street"));
    Student copy = original.deepCopy();
    // 此时 original 和 copy 是两个不同的对象,并且它们的 address 引用的是两个不同的 Address 对象实例
    

通过上述方式,可以根据需要选择浅拷贝或者深拷贝来复制对象,从而满足不同的需求。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超维Ai编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值