[转]浅谈java中的clone

原文链接http://blog.csdn.net/morethinkmoretry/article/details/5884533

 

在java里提到clone技术,大家可能很快就会想到java.lang.Cloneable这个接口。大家可能都知道:所有具有clone功能的类都有一个特性,那就是它直接或间接地实现了Cloneable接口。但是仔细一翻看Cloneable接口的源码发现一个方法都没有。其实子类实现Cloneable接口,相当于一个标示:既在jvm的方法区中存放了想具有clone功能的类(子类)实现了的接口列表,如果接口列表中含有java.lang.Cloneable这个值,jvm将调用类中的clone();显然clone()必须重写自所有java类的祖宗类Object的这个方法;

这个我们从Object类中clone()方法申明中可以略窥一二

 

 

  1.  * @return     a clone of this instance.  
  2.  * @exception  CloneNotSupportedException  if the object's class does not  
  3.  *               support the <code>Cloneable</code> interface. Subclasses  
  4.  *               that override the <code>clone</code> method can also  
  5.  *               throw this exception to indicate that an instance cannot  
  6.  *               be cloned.  
  7.  * @see java.lang.Cloneable  
  8.  */  
  9. protected native Object clone() throws CloneNotSupportedException;  
* @return a clone of this instance. * @exception CloneNotSupportedException if the object's class does not * support the <code>Cloneable</code> interface. Subclasses * that override the <code>clone</code> method can also * throw this exception to indicate that an instance cannot * be cloned. * @see java.lang.Cloneable */ protected native Object clone() throws CloneNotSupportedException; 

 

 

 

这段源码的@exception部分的描述内容明确指出对象类必须支持Cloneable接口,否则即使派生类覆盖了Object#clone()方法,也会抛出CloneNotSupportedException这个异常。

为什么一定要覆盖Object类的clone方法呢?主要有俩方面因素:

1:我们看上面的Object类的clone方法源码发现该方法的访问控制权限为protected ,如果我们不在子类中覆盖它,将访问权限变为 public;那么就使得clone方法对使用者不可见。

2:覆盖方法,显然是重写重写定制该方法行为了,对吧?比如我们下面时候的深浅拷贝

 有了上面这段clone技术的基本的知识后,我们先了解下clone技术的原理。查阅下资料可知:

Object类的clone方法原理是从内从中(具体说是对内存)以二级制流的方式进行拷贝,重新分配一个内存模块来克隆一个新的java对象。

我们从程序上来验证这点:

 

 

 

  1. public class TestClone implements Cloneable {  
  2.     public TestClone() {  
  3.         System.out.println("    构造函数被执行了");  
  4.     }  
  5.     public TestClone clone() {  
  6.         TestClone testClone = null;  
  7.         try {  
  8.             testClone = (TestClone) super.clone();  
  9.         } catch (CloneNotSupportedException e) {  
  10.             e.printStackTrace();  
  11.         }  
  12.         return testClone;  
  13.     }  
  14.     public static void main(String[] args) {  
  15.         System.out.println("第一次开始执行构造函数");  
  16.         TestClone testClone = new TestClone();  
  17.         System.out.println("看clone是否会执行构造函数");  
  18.         TestClone testClone1 = testClone.clone();  
  19.     }  
  20. }  
public class TestClone implements Cloneable { public TestClone() { System.out.println(" 构造函数被执行了"); } public TestClone clone() { TestClone testClone = null; try { testClone = (TestClone) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return testClone; } public static void main(String[] args) { System.out.println("第一次开始执行构造函数"); TestClone testClone = new TestClone(); System.out.println("看clone是否会执行构造函数"); TestClone testClone1 = testClone.clone(); } } 

 

 

运行结果如下:

第一次开始执行构造函数

    构造函数被执行了

看clone是否会执行构造函数

显然使用clone方法克隆java对象的时候,并没有去执行java类的构造方法,而是直接从堆内存中复制出新内存对象。

浅clone和深clone

在解释什么是深拷贝和浅拷贝之前,我们先来看看例子

 

[c-sharp] view plain copy print ?
  1. import java.util.ArrayList;  
  2. public class TestShenQianClone implements Cloneable {  
  3.     private ArrayList<String> arrayList = new ArrayList<String>();  
  4.     public TestShenQianClone clone() {  
  5.         TestShenQianClone testClone = null;  
  6.         try {  
  7.            testClone = (TestShenQianClone) super.clone();  
  8.         } catch (CloneNotSupportedException e) {  
  9.             e.printStackTrace();  
  10.         }  
  11.         return testClone;  
  12.     }  
  13.     public ArrayList<String> getArrayList() {  
  14.         return this.arrayList;  
  15.     }  
  16.     public void setValue(String value) {  
  17.         arrayList.add(value);  
  18.     }  
  19.     public static void main(String[] args) {  
  20.         TestShenQianClone testClone = new TestShenQianClone();  
  21.         testClone.setValue("张三");  
  22.         TestShenQianClone testClone1 = testClone.clone();  
  23.         testClone1.setValue("李四");  
  24.         System.out.println(testClone.getArrayList());  
  25.         System.out.println(testClone1.getArrayList());  
  26.     }  
  27. }  
import java.util.ArrayList; public class TestShenQianClone implements Cloneable { private ArrayList<String> arrayList = new ArrayList<String>(); public TestShenQianClone clone() { TestShenQianClone testClone = null; try { testClone = (TestShenQianClone) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return testClone; } public ArrayList<String> getArrayList() { return this.arrayList; } public void setValue(String value) { arrayList.add(value); } public static void main(String[] args) { TestShenQianClone testClone = new TestShenQianClone(); testClone.setValue("张三"); TestShenQianClone testClone1 = testClone.clone(); testClone1.setValue("李四"); System.out.println(testClone.getArrayList()); System.out.println(testClone1.getArrayList()); } }  

 

 

运行结果为:

[张三, 李四]

[张三, 李四]

为什么会这样?因为Java做了一个偷懒的浅拷贝,Object类提供的clone方法只是拷贝本对象(即:原型类型数据(int,short,bit,long等)和不可变对象(String)以及可变对象引用)。

对于上面的该例子:只是拷贝了arrayList引用,该new ArrayList<String>()数组对象还是公用同一份。类似下图

 

  

 

 

而我们希望不是俩个对象公用同一对象内存数据。我们希望一个对象一个。

那如果要达到这样的效果,我们该如何做呢?

看看调整后的代码

 

 

 

  1. import java.util.ArrayList;  
  2. public class TestShenQianClone implements Cloneable {  
  3.     private ArrayList<String> arrayList = new ArrayList<String>();  
  4.     public TestShenQianClone clone() {  
  5.         TestShenQianClone testClone = null;  
  6.         try {  
  7.             testClone = (TestShenQianClone) super.clone();  
  8.             testClone.arrayList = (ArrayList<String>) this.arrayList.clone();  
  9.         } catch (CloneNotSupportedException e) {  
  10.             e.printStackTrace();  
  11.         }  
  12.         return testClone;  
  13.     }  
  14.     public ArrayList<String> getArrayList() {  
  15.         return this.arrayList;  
  16.     }  
  17.     public void setValue(String value) {  
  18.         arrayList.add(value);  
  19.     }  
  20.     public static void main(String[] args) {  
  21.         TestShenQianClone testClone = new TestShenQianClone();  
  22.         testClone.setValue("张三");  
  23.         TestShenQianClone testClone1 = testClone.clone();  
  24.         testClone1.setValue("李四");  
  25.         System.out.println(testClone.getArrayList());  
  26.         System.out.println(testClone1.getArrayList());  
  27.     }  
  28. }  
import java.util.ArrayList; public class TestShenQianClone implements Cloneable { private ArrayList<String> arrayList = new ArrayList<String>(); public TestShenQianClone clone() { TestShenQianClone testClone = null; try { testClone = (TestShenQianClone) super.clone(); testClone.arrayList = (ArrayList<String>) this.arrayList.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return testClone; } public ArrayList<String> getArrayList() { return this.arrayList; } public void setValue(String value) { arrayList.add(value); } public static void main(String[] args) { TestShenQianClone testClone = new TestShenQianClone(); testClone.setValue("张三"); TestShenQianClone testClone1 = testClone.clone(); testClone1.setValue("李四"); System.out.println(testClone.getArrayList()); System.out.println(testClone1.getArrayList()); } }  

 

 

 

程序改动点有一开始的

testClone = (TestShenQianClone) super.clone();

变成了现在的

testClone = (TestShenQianClone) super.clone();
testClone.arrayList = (ArrayList<String>) this.arrayList.clone();

那看看效果如果,看运行结果:

[张三]
[张三, 李四]

效果出来了吧。testClone的数据(张三)拷贝到了testClone1中了,但是testClone1的数据(李四)只是他自己的。

因此:如果java类中含有可变类对象引用,要执行拷贝的话,需要分别对这些可变类对象进行拷贝,才能达到深度拷贝。

可变类对象引用解释:这个类不是final类型的对象应用。其中String就不是这种,而是不可变类型引用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值