Java深入理解深拷贝和浅拷贝区别


Java中使用对象的父类的clone方法和直接赋值都是浅拷贝,例如:

class Student implements Cloneable {
    ...
    public Object clone() throws CloneNotSupportedException {
        Object object = super.clone();
        return object;
    }
}

public class Main {
    public static void main(String[] args) throws CloneNotSupportedException {
        Student student1 = new Student();
        Student student2 = (Student)student1.clone();
    }
}

class Student{
    ...
}

public class Main {
    public static void main(String[] args) {
        Student student1 = new Student();
        Student student2 = student1;
    }
}

效果是一样的。

参考:

Java深入理解深拷贝和浅拷贝区别


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM