1 protected native Object clone() throws CloneNotSupportedException;
java中的native關鍵字表示這個方法是個本地方法,【java native說明】。而且native修飾的方法執行效率比非native修飾的高。
一個類在覆蓋clone()方法時候,需要修改成public訪問修飾符,這樣才能保證其他所有的類都能夠訪問這個類的這個方法。
一個類想要覆蓋clone()方法,必須本身實現java.lang.Cloneable接口,否則會拋出CloneNotSupportedException異常。
注:我們這裡的對象特指復雜類型的。
我們知道,java中的復雜類型的對象都是引用類型,他們往往存的都是對象的內存地址。因此我們不能僅僅通過 = 操作符這樣簡單的賦值操作。我們將一個對象a 賦值給另一個對象b ,我們僅僅是將對象a 的內存地址賦值給b ,使得他們兩個對象都是指向的同一個內存地址。這樣的後果是,對其中一個對象的修改之後都會影響到另一個對象。如下圖表示:
1 Person p1 = new Person(); 2 Person p2 = p1;
使用clone()方法,可以快速的創建一個對象的副本,並且兩個對象指向不同的內存地址。如下圖表示:
1 Person p1 = new Person(); 2 Person p2 = p1.clone();
shallow clone是指只clone對象本身,不clone對象裡的字段。只調用super.clone(),只是shallow clone。雖然拷貝之後的對象是指向了不同的內存地址,但是對象裡面的字段還是和之前的對象指向同一個內存地址。
1 public class ShallowClone implements Cloneable { 2 3 public String name; 4 public int age; 5 public Person person; 6 7 public ShallowClone() { 8 } 9 10 public ShallowClone(String name, int age, Person person) { 11 this.name = name; 12 this.age = age; 13 this.person = person; 14 } 15 16 @Override 17 public ShallowClone clone() { 18 ShallowClone c = null; 19 try { 20 c = (ShallowClone) super.clone(); 21 return c; 22 } catch (CloneNotSupportedException e) { 23 e.printStackTrace(); 24 } 25 return c; 26 } 27 28 public static void main(String[] args) { 29 Person p = new Person(); 30 p.name = "p"; 31 p.age = 10; 32 33 ShallowClone c1 = new ShallowClone("Jim", 18, p); 34 System.out.printf("before clone: c1 = %s, c1.person = %s\n", c1, c1.person); 35 ShallowClone c2 = c1.clone(); 36 System.out.printf("after clone: c2 = %s, c2.person = %s\n", c2, c2.person); 37 } 38 }
運行main()輸出:
before clone: c1 = cre.sample.test.object.ShallowClone@558385e3, c1.person = cre.sample.test.Person@2dcb25f1 after clone: c2 = cre.sample.test.object.ShallowClone@742808b3, c2.person = cre.sample.test.Person@2dcb25f1
說明淺拷貝,ShallowClone對象內存地址改變了,但是對象裡的Person字段內存地址沒有改變;
deep clone則是指在clone對象本身的同時,也clone對象裡面的字段。
1 /** 2 * deep clone代碼示例 3 * Created by CreGu on 2016/6/9. 4 */ 5 public class DeepClone implements Cloneable { 6 public String name; 7 public int age; 8 public Person person; 9 10 public DeepClone() { 11 } 12 13 public DeepClone(String name, int age, Person person) { 14 this.name = name; 15 this.age = age; 16 this.person = person; 17 } 18 19 @Override 20 public DeepClone clone() { 21 DeepClone c = null; 22 try { 23 c = (DeepClone) super.clone(); 24 c.person = person.clone(); 25 return c; 26 } catch (CloneNotSupportedException e) { 27 e.printStackTrace(); 28 } 29 return c; 30 } 31 32 public static void main(String[] args) { 33 Person p = new Person(); 34 p.name = "p"; 35 p.age = 10; 36 37 DeepClone c1 = new DeepClone("Jim", 18, p); 38 System.out.printf("before clone: c1 = %s, c1.person = %s\n", c1, c1.person); 39 DeepClone c2 = c1.clone(); 40 System.out.printf("after clone: c2 = %s, c2.person = %s\n", c2, c2.person); 41 } 42 }
運行main()輸出:
before clone: c1 = cre.sample.test.object.DeepClone@558385e3, c1.person = cre.sample.test.Person@2dcb25f1
after clone: c2 = cre.sample.test.object.DeepClone@742808b3, c2.person = cre.sample.test.Person@70535b58
說明深拷貝,DeepClone對象內存地址改變了,但是對象裡的Person字段內存地址也改變了;