在開始學習深克隆和淺克隆以前,咱們先來看下面代碼,有什麼問題?java
class CloneTest { public static void main(String[] args) throws CloneNotSupportedException { // 等號賦值( 基本類型) int number = 6; int number2 = number; // 修改 number2 的值 number2 = 9; System.out.println("number:" + number); System.out.println("number2:" + number2); // 等號賦值(對象) Dog dog = new Dog(); dog.name = "旺財"; dog.age = 5; Dog dog2 = dog; // 修改 dog2 的值 dog2.name = "大黃"; dog2.age = 3; System.out.println(dog.name + "," + dog.age + "歲"); System.out.println(dog2.name + "," + dog2.age + "歲"); } }
程序執行結果:git
number:6 number2:9 大黃,3歲 大黃,3歲
能夠看出,若是使用等號複製時,對於值類型來講,彼此之間的修改操做是相對獨立的,而對於引用類型來講,由於複製的是引用對象的內存地址,因此修改其中一個值,另外一個值也會跟着變化,原理以下圖所示:
所以爲了防止這種問題的發生,就要使用對象克隆來解決引用類型複製的問題。github
淺克隆的默認實現方法是 clone(),實現代碼以下:面試
class CloneTest { public static void main(String[] args) throws CloneNotSupportedException { Dog dog = new Dog(); dog.name = "旺財"; dog.age = 5; // 克隆 Dog dog3 = (Dog) dog.clone(); dog3.name = "小白"; dog3.age = 2; System.out.println(dog.name + "," + dog.age + "歲"); System.out.println(dog3.name + "," + dog3.age + "歲"); } } class Dog implements Cloneable { public String name; public int age; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }
程序執行結果:ide
旺財,5歲 小白,2歲
能夠看出使用克隆就能夠解決引用類型複製的問題了,原理以下圖所示:
以上這種複製方式叫作淺克隆。
淺克隆的實現條件:須要克隆的對象必須實現 Cloneable
接口,並重寫 clone()
方法,便可實現對此對象的克隆。
然而使用淺克隆也會存在一個問題,請參考如下代碼。函數
class CloneTest { public static void main(String[] args) throws CloneNotSupportedException { DogChild dogChild = new DogChild(); dogChild.name = "二狗"; Dog dog4 = new Dog(); dog4.name = "大黃"; dog4.dogChild = dogChild; Dog dog5 = (Dog) dog4.clone(); dog5.name = "旺財"; dog5.dogChild.name = "狗二"; System.out.println("dog name 4:"+dog4.name); System.out.println("dog name 5:"+dog5.name); System.out.println("dog child name 4:"+dog4.dogChild.name); System.out.println("dog child name 5:"+dog5.dogChild.name); } } class Dog implements Cloneable { public String name; public DogChild dogChild; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } } class DogChild { public String name; }
程序執行結果:性能
dog name 4:大黃 dog name 5:旺財 dog child name 4:狗二 dog child name 5:狗二
也就是說淺克隆,只會複製對象的值類型,而不會複製對象的引用類型。緣由以下圖所示:
要處理引用類型不被複制的問題,就要使用到深克隆。學習
定義:深克隆就是複製整個對象信息,包含值類型和引用類型。
深克隆的實現方式一般包含如下兩種。操作系統
深克隆實現方式一:序列化code
實現思路:先將要拷貝對象寫入到內存中的字節流中,而後再從這個字節流中讀出剛剛存儲的信息,做爲一個新對象返回,那麼這個新對象和原對象就不存在任何地址上的共享,天然實現了深拷貝。請參考如下代碼:
class CloneTest { public static void main(String[] args) throws CloneNotSupportedException { BirdChild birdChild = new BirdChild(); birdChild.name = "小小鳥"; Bird bird = new Bird(); bird.name = "小鳥"; bird.birdChild = birdChild; // 使用序列化克隆對象 Bird bird2 = CloneUtils.clone(bird); bird2.name = "黃雀"; bird2.birdChild.name = "小黃雀"; System.out.println("bird name:" + bird.name); System.out.println("bird child name:" + bird.birdChild.name); System.out.println("bird name 2:" + bird2.name); System.out.println("bird child name 2:" + bird2.birdChild.name); } } class CloneUtils { public static <T extends Serializable> T clone(T obj) { T cloneObj = null; try { //寫入字節流 ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bo); oos.writeObject(obj); oos.close(); //分配內存,寫入原始對象,生成新對象 ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());//獲取上面的輸出字節流 ObjectInputStream oi = new ObjectInputStream(bi); //返回生成的新對象 cloneObj = (T) oi.readObject(); oi.close(); } catch (Exception e) { e.printStackTrace(); } return cloneObj; } }
程序執行結果:
bird name:小鳥 bird child name:小小鳥 bird name 2:黃雀 bird child name 2:小黃雀
深克隆實現方式二:全部引用類型都實現克隆
class SerializableTest { public static void main(String[] args) throws IOException, ClassNotFoundException { ParrotChild parrotChild = new ParrotChild(); parrotChild.name = "小鸚鵡"; Parrot parrot = new Parrot(); parrot.name = "大鸚鵡"; parrot.parrotChild = parrotChild; // 克隆 Parrot parrot2 = (Parrot) parrot.clone(); parrot2.name = "老鸚鵡"; parrot2.parrotChild.name = "少鸚鵡"; System.out.println("parrot name:" + parrot.name); System.out.println("parrot child name:" + parrot.parrotChild.name); System.out.println("parrot name 2:" + parrot2.name); System.out.println("parrot child name 2:" + parrot2.parrotChild.name); } } class Parrot implements Cloneable { public String name; public ParrotChild parrotChild; @Override protected Object clone() throws CloneNotSupportedException { Parrot bird = (Parrot) super.clone(); bird.parrotChild = (ParrotChild) parrotChild.clone(); return bird; } } class ParrotChild implements Cloneable { public String name; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }
程序執行結果:
parrot name:大鸚鵡 parrot child name:小鸚鵡 parrot name 2:老鸚鵡 parrot child name 2:少鸚鵡
答:好處包含如下幾點:
clone() 源代碼,以下圖:
答:區別主要在對引用類型的複製上,具體信息以下:
深克隆:複製整個對象,包含值類型和引用類型。
答:克隆的對象實現 Cloneable 接口,並重寫 clone() 方法就能夠實現淺克隆了。
import java.util.Arrays; class CloneTest { public static void main(String[] args) throws CloneNotSupportedException { CloneObj cloneObj = new CloneObj(); cloneObj.name = "老王"; cloneObj.age = 30; cloneObj.sistersAge = new int[]{18, 19}; CloneObj cloneObj2 = (CloneObj) cloneObj.clone(); cloneObj2.name = "磊哥"; cloneObj2.age = 33; cloneObj2.sistersAge[0] = 20; System.out.println(cloneObj.name + "|" + cloneObj2.name); System.out.println(cloneObj.age + "|" + cloneObj2.age); System.out.println(Arrays.toString(cloneObj.sistersAge) + "|" + Arrays.toString(cloneObj2.sistersAge)); } } class CloneObj implements Cloneable { public String name; public int age; public int[] sistersAge; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }
答:執行結果以下。
老王|磊哥 30|33 [20, 19]|[20, 19]
答:通常實現方式有如下兩種:
全部引用類型都實現克隆,從而實現深克隆。
答:雖然全部類都是 Object 的子類,但由於 Object 中的 clone() 方法被聲明爲 protected 訪問級別,因此非 java.lang 包下的其餘類是不能直接使用的。所以要想實現克隆功能,就必須實現 Cloneable,並重寫 clone() 方法才行。
答:先將原對象序列化到內存的字節流中,再從字節流中反序列化出剛剛存儲的對象,這個新對象和原對象就不存在任何地址上的共享,這樣就實現了深克隆。
調用 Object 類中的 clone() 方法默認是淺克隆,淺克隆只能複製值類型,不能複製引用類型,所以更多的場景下咱們須要深克隆,深克隆一般的實現方式有兩種:序列化方式或把全部的引用類型都實現克隆。
點擊此處下載本文源碼
本文來自《Java面試題全解析》