您的位置:首页 > 编程语言 > Java开发

java clone技术 浅谈 转

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

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

* @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对象。

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

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类的构造方法,而是直接从堆内存中复制出新内存对象。

浅[b]clone和深clone[/b]

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

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>()数组对象还是公用同一份。类似下图



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

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

看看调整后的代码

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就不是这种,而是不可变类型引用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: