乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      關于Java的clone()

       燮羽 2010-11-06

      相關要點:

      1.必須實現(xiàn)Cloneable接口,這個接口只是一個標識;如果不實現(xiàn),調(diào)用了clone(),運行時會報CloneNotSupportedException

      2.clone是Object的方法,標識為protected,子類必須重寫,標識符可改為public

      3.對于jdk1.5,clone可以返回相應類的類型或Object;對于1.4,只能返回Object

      4.注意深復制和淺復制

      淺復制

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

      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. }  
       

       

      結果:

      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)對其他類及自定義類,默認采用的是淺復制

      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. }  

       

      結果:

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

      深復制

      對于其中非基本類型的字段,必須明確進行其賦值

      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. }  

       

      結果:

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

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多