一、簡介
Cloneable介面是標記介面之一,即沒有任何方法的介面,作用是告訴Java運行時環境,任何實現了Cloneable介面的類的實例,可以使用Object類中的clone方法進行克隆。
使用clone()方法可以避免對象引用淺拷貝的問題,實現等價對象的複製。clone()方法會在堆內存中為新的對象分配空間,並將原始對象的狀態複製到新對象中,同時產生一個新的對象引用。
二、實現Cloneable介面
Cloneable介面並沒有任何方法,實現Cloneable介面的類必須重寫Object類的clone()方法。
在重寫clone()方法時,需要注意以下事項:
1、clone()方法需要使用public修飾符,這是因為在Object類中,定義了protected類型的clone()方法,如果要在子類中重寫該方法,必須使用public修飾符覆蓋該方法;
2、需要調用Object類的clone()方法,返回Object類型,然後進行強制類型轉換並返回。
public class MyClass implements Cloneable { private int count; private String name; public MyClass(int count, String name) { this.count = count; this.name = name; } @Override public Object clone() throws CloneNotSupportedException { MyClass cloned = (MyClass) super.clone(); return cloned; } }
三、正確的深拷貝
clone()方法進行的是淺拷貝,即對象的引用被複制,但是對象的實例並沒有被複制。如果需要進行深拷貝,需要在實現clone()方法的時候進行操作。
public class MyClass implements Cloneable { private int count; private String name; private List list; public MyClass(int count, String name, List list) { this.count = count; this.name = name; this.list = list; } @Override public Object clone() throws CloneNotSupportedException { MyClass cloned = (MyClass) super.clone(); List tempList = new ArrayList(); for (String str : this.list) { tempList.add(str); } cloned.list = tempList; return cloned; } }
在上面的例子中,MyClass的實例包含一個List類型的成員變數,如果進行淺拷貝,那麼新克隆出來的實例中的list成員變數的引用還是指向原來的那個List實例,會出現修改一個對象的情況影響到另一個對象的現象。因此,需要重新創建一個List實例,並將原來的值複製。
四、小結
Cloneable介面作用是標記介面,用於告訴Java虛擬機,實現了此介面的類可以被克隆;實現克隆需要重寫Object類的clone()方法;
進行深拷貝需要在重寫的clone()方法中手動進行數據的複製。
完整代碼示例:
public class MyClass implements Cloneable { private int count; private String name; public MyClass(int count, String name) { this.count = count; this.name = name; } @Override public Object clone() throws CloneNotSupportedException { MyClass cloned = (MyClass) super.clone(); return cloned; } } public class DeepCloneClass implements Cloneable { private int age; private List list; public DeepCloneClass(int age, List list) { this.age = age; this.list = list; } @Override public Object clone() throws CloneNotSupportedException { DeepCloneClass cloned = (DeepCloneClass) super.clone(); List tempList = new ArrayList(); for (String str : this.list) { tempList.add(str); } cloned.list = tempList; return cloned; } } public class TestClass { public static void main(String[] args) throws CloneNotSupportedException { MyClass obj1 = new MyClass(10, "obj1"); MyClass obj2 = (MyClass) obj1.clone(); System.out.println(obj1 == obj2); // false System.out.println(obj1.equals(obj2)); // true List list1 = new ArrayList(); list1.add("a"); list1.add("b"); list1.add("c"); DeepCloneClass obj3 = new DeepCloneClass(20, list1); DeepCloneClass obj4 = (DeepCloneClass) obj3.clone(); System.out.println(obj3 == obj4); // false System.out.println(obj3.equals(obj4)); // true obj3.getList().add("d"); System.out.println(obj3.equals(obj4)); // false } }
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/302834.html