分享

关于Java的clone()

 燮羽 2010-11-06

相关要点:

1.必须实现Cloneable接口,这个接口只是一个标识;如果不实现,调用了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现值");  
  22.         print(a);  
  23.         System.out.println("b现值");  
  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现值
a=12
b=hahaa
c=14.11
b现值
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现值");  
  25.         print(a);  
  26.         System.out.println("b现值");  
  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现值
a=12
b=hahab
b现值
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现值");  
  30.         print(a);  
  31.         System.out.println("b现值");  
  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现值
a=12
b=hahaa
b现值
a=13
b=hahab

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多