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深入理解深拷贝和浅拷贝区别

相关文章:

  • 2022-12-23
猜你喜欢
  • 2021-06-17
  • 2022-12-23
  • 2022-12-23
  • 2021-08-03
  • 2021-05-27
  • 2021-12-26
相关资源
相似解决方案