小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

關(guān)于Java的clone()

 燮羽 2010-11-06

相關(guān)要點(diǎn):

1.必須實(shí)現(xiàn)Cloneable接口,這個(gè)接口只是一個(gè)標(biāo)識(shí);如果不實(shí)現(xiàn),調(diào)用了clone(),運(yùn)行時(shí)會(huì)報(bào)CloneNotSupportedException

2.clone是Object的方法,標(biāo)識(shí)為protected,子類必須重寫,標(biāo)識(shí)符可改為public

3.對(duì)于jdk1.5,clone可以返回相應(yīng)類的類型或Object;對(duì)于1.4,只能返回Object

4.注意深復(fù)制和淺復(fù)制

淺復(fù)制

(1)對(duì)于int,double,Double,String等基本類型,super.clone()是采用的深復(fù)制

  1. public class TestShallow implements Cloneable {  
  2.     public int a;  
  3.     public String b;  
  4.     public Double c;  
  5.     public TestShallow clone() throws CloneNotSupportedException{  
  6.         return (TestShallow)super.clone();  
  7.     }  
  8. }  
  9. class Test {  
  10.     public static void main(String[] args) throws CloneNotSupportedException {  
  11.         TestShallow a = new TestShallow();  
  12.         a.a = 12;  
  13.         a.b = "hahaa";  
  14.         a.c = 14.11;  
  15.         System.out.println("a原值");  
  16.         print(a);  
  17.         TestShallow b = a.clone();  
  18.         b.a = 13;  
  19.         b.b = "hahab";  
  20.         b.c = 15.11;  
  21.         System.out.println("a現(xiàn)值");  
  22.         print(a);  
  23.         System.out.println("b現(xiàn)值");  
  24.         print(b);  
  25.     }  
  26.       
  27.     public static void print(TestShallow a) {  
  28.         System.out.println("a=" + a.a);  
  29.         System.out.println("b=" + a.b);  
  30.         System.out.println("c=" + a.c);  
  31.     }  
  32. }  
 

 

結(jié)果:

a原值
a=12
b=hahaa
c=14.11
a現(xiàn)值
a=12
b=hahaa
c=14.11
b現(xiàn)值
a=13
b=hahab
c=15.11

(2)對(duì)其他類及自定義類,默認(rèn)采用的是淺復(fù)制

  1. class A {  
  2.     public String a;  
  3. }  
  4. public class TestShallow1 implements Cloneable {  
  5.     public int a;  
  6.     public A b;  
  7.     public TestShallow1() {  
  8.         b = new A();  
  9.     }  
  10.     public TestShallow1 clone() throws CloneNotSupportedException{  
  11.         return (TestShallow1)super.clone();  
  12.     }  
  13. }  
  14. class Test1 {  
  15.     public static void main(String[] args) throws CloneNotSupportedException {  
  16.         TestShallow1 a = new TestShallow1();  
  17.         a.a = 12;  
  18.         a.b.a = "hahaa";  
  19.         System.out.println("a原值");  
  20.         print(a);  
  21.         TestShallow1 b = a.clone();  
  22.         b.a = 13;  
  23.         b.b.a = "hahab";  
  24.         System.out.println("a現(xiàn)值");  
  25.         print(a);  
  26.         System.out.println("b現(xiàn)值");  
  27.         print(b);  
  28.     }  
  29.       
  30.     public static void print(TestShallow1 a) {  
  31.         System.out.println("a=" + a.a);  
  32.         System.out.println("b=" + a.b.a);  
  33.     }  
  34. }  

 

結(jié)果:

a原值
a=12
b=hahaa
a現(xiàn)值
a=12
b=hahab
b現(xiàn)值
a=13
b=hahab

深復(fù)制

對(duì)于其中非基本類型的字段,必須明確進(jìn)行其賦值

  1. class A implements Cloneable {  
  2.     public String a;  
  3.     public A clone() throws CloneNotSupportedException{  
  4.         return (A)super.clone();  
  5.     }  
  6. }  
  7. public class TestDeep implements Cloneable {  
  8.     public int a;  
  9.     public A b;  
  10.     public TestDeep() {  
  11.         b = new A();  
  12.     }  
  13.     public TestDeep clone() throws CloneNotSupportedException{  
  14.         TestDeep clone = (TestDeep)super.clone();  
  15.         clone.b = this.b.clone();  
  16.         return clone;  
  17.     }  
  18. }  
  19. class Test1 {  
  20.     public static void main(String[] args) throws CloneNotSupportedException {  
  21.         TestDeep a = new TestDeep();  
  22.         a.a = 12;  
  23.         a.b.a = "hahaa";  
  24.         System.out.println("a原值");  
  25.         print(a);  
  26.         TestDeep b = a.clone();  
  27.         b.a = 13;  
  28.         b.b.a = "hahab";  
  29.         System.out.println("a現(xiàn)值");  
  30.         print(a);  
  31.         System.out.println("b現(xiàn)值");  
  32.         print(b);  
  33.     }  
  34.       
  35.     public static void print(TestDeep a) {  
  36.         System.out.println("a=" + a.a);  
  37.         System.out.println("b=" + a.b.a);  
  38.     }  
  39. }  

 

結(jié)果:

a原值
a=12
b=hahaa
a現(xiàn)值
a=12
b=hahaa
b現(xiàn)值
a=13
b=hahab

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多