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; } }
效果是一樣的。
參考: