Cloneable的深拷贝与浅拷贝

8 篇文章 0 订阅

一、Cloneable是啥
Cloneable是java的一个接口,接口里并没有需要强制实现的方法,因此这是一个标记性接口,如果要使用clone方法,就必须实现Cloneable接口,否则会抛出CloneNotSupportedException异常。

    // BEGIN Android-changed: Use native local helper for clone()
    // Checks whether cloning is allowed before calling native local helper.
    // protected native Object clone() throws CloneNotSupportedException;
    protected Object clone() throws CloneNotSupportedException {
        if (!(this instanceof Cloneable)) {
            throw new CloneNotSupportedException("Class " + getClass().getName() +
                                                 " doesn't implement Cloneable");
        }

        return internalClone();
    }

    /*
     * Native helper method for cloning.
     */
    @FastNative
    private native Object internalClone();
    // END Android-changed: Use native local helper for clone()

通过源码可知internalClone方法体是在native层去实现的,并且在调用clone方法时会去校验是否实现了Cloneable接口

二、深拷贝和浅拷贝是如何体现的?

深浅拷贝的区别个人觉得是引用地址的传递还是真正的对象拷贝,下面通过代码来解释说明。

public class Teacher implements Cloneable{
    public String name;
    public int age;
    public Student student;
    
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class Student {
    public String name;
    public int age;
}
        Teacher teacher = new Teacher();
        teacher.name="老师";
        teacher.age = 100;

        Student student = new Student();
        student.name="学生";
        student.age=90;

        teacher.student = student;

        try {
            Teacher cloneTeacher = (Teacher) teacher.clone();
            //两者hash值不同
            Log.e("teacher",teacher.hashCode()+"  "+cloneTeacher.hashCode());
            //两者hash值相同
            Log.e("teacher",teacher.student.hashCode()+"  "+cloneTeacher.student.hashCode());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

通过打印日志发现,通过clone方法,能完全复制一个新对象,可以通过gson转换成json数据查看两者的属性值是一样的,但是你会发现teacher跟cloneTeacher虽然是不同的对象,但是他们的student却是同一个对象,因为两者hash值是一样的,你也可以通过修改cloneTeacher里student的name值,然后去打印查看teacher里的student的name值是否也随之改变。这其实就是浅拷贝,可以理解为student对象没有重新创建,只是引用地址还是指向原有的,克隆后的teacher对象共享同一个student。

接下来我们尝试修改上面的代码

public class Teacher implements Cloneable{
    public String name;
    public int age;
    public Student student;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Teacher teacher = (Teacher) super.clone();
        //这里对student进行深度克隆
        teacher.student = (Student) student.clone();
        return teacher;

    }
}
public class Student implements Cloneable{
    public String name;
    public int age;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //因为teacher对student进行了clone方法的调用,所以这里也要重写该方法,并实现Cloneable接口
        return super.clone();
    }
}

代码修改完毕后再次进行上面的log验证

//两者hash值不同
Log.e("teacher",teacher.hashCode()+"  "+cloneTeacher.hashCode());
//两者hash值不同
Log.e("teacher",teacher.student.hashCode()+"  "+cloneTeacher.student.hashCode());

因此可以判断此时两者的student对象不是指向同一个了,这就是深拷贝

总结:如果对克隆对象只是取值操作的话,深浅拷贝没有区别,但是一旦涉及对克隆对象属性的修改这种浅拷贝方式就很危险,影响了其他业务对原始数据的使用。因此日常开发中要注意,尽量考虑用深拷贝方法,采用这种套娃的方式,在clone方法里,依次对引用类型进行手动clone并赋值。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值