为什么要使用克隆?如何实现对象克隆?深拷贝和浅拷贝区别是什么

有时需要复制一个对象,并且希望保留原有的对象进行接下来的操作。这时就需要使用克隆。

如何实现对象克隆?
  1. 实现Cloneable接口并重写clone方法
  • 实现​​Cloneable​​接口后,可以调用​​Object.clone()​​方法来创建一个新的对象。
  1. 通过序列化和反序列化实现深克隆
  • 将对象转换为字节流(序列化),然后再将字节流转回对象(反序列化)。
  1. 使用BeanUtils、Apache Commons或Spring等工具类库提供的bean工具
  • 这些工具提供了方便的克隆功能,但通常只能实现浅克隆。
深拷贝和浅拷贝的区别是什么?

  • 浅拷贝:仅复制基本类型变量,不复制引用类型的变量。
  • 深拷贝:既复制基本类型变量,又复制引用类型的变量。

示例代码解析
浅拷贝示例
public class Test {
    public static void main(String[] args) {
        Student student = new Student();
        student.setId(1);
        student.setName("张三");
        Address address = new Address();
        address.setDescribe("北京市朝阳区");
        student.setAddress(address);

        try {
            Student cloneStu = (Student) student.clone();
            System.out.println("------------------------------浅拷贝----------------------------------");
            System.out.println("student == cloneStu: " + (student == cloneStu));
            System.out.println("student.getAddress() == cloneStu.getAddress(): " + (student.getAddress() == cloneStu.getAddress()));
            System.out.println("student.getAddress().getDescribe() == cloneStu.getAddress().getDescribe(): " + (student.getAddress().getDescribe() == cloneStu.getAddress().getDescribe()));

            // 修改属性前
            System.out.println(student.toString());
            System.out.println(cloneStu.toString());

            // 修改属性
            student.setName("李四");
            address.setDescribe("上海市浦东新区");

            // 修改属性后
            System.out.println(student.toString());
            System.out.println(cloneStu.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
深拷贝示例
public class DeepCloneTest implements Cloneable {
    private String name;
    private int age;

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

    public static void main(String[] args) {
        DeepCloneTest test = new DeepCloneTest();
        test.name = "张三";
        test.age = 20;

        try {
            DeepCloneTest cloneTest = (DeepCloneTest) test.clone();
            System.out.println("------------------------------深拷贝----------------------------------");
            System.out.println("test == cloneTest: " + (test == cloneTest));
            System.out.println("test.name == cloneTest.name: " + (test.name == cloneTest.name));
            System.out.println("test.age == cloneTest.age: " + (test.age == cloneTest.age));

            // 修改属性前
            System.out.println(test.toString());
            System.out.println(cloneTest.toString());

            // 修改属性
            test.name = "李四";
            test.age = 25;

            // 修改属性后
            System.out.println(test.toString());
            System.out.println(cloneTest.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值