---
title: "JAVA Clone机制"
author: "water84222"
date: "2008-11-10T16:24:58Z"
tags: []
slug: "java-clone"
draft: false
lastmod: "2008-11-12T16:43:55Z"
id: 265113
url: "https://onlyLove.iteye.com/blog/265113"
---

1，什么是Clone ？

简单地说， Clone 就是对于给定的一个对象实例 o ，得到另一个对象实例 o’ ： o 与 o’ 类

型相同（ o.getClass() == o’.getClass() ），内容相同（对于 o/o’ 中的字段 f ，如果 f 是基本数据类型，则 o.f == o’.f ；如果 f 是对象引用，则 o.f == o’.f 或 o.f 指向的对象与 o’.f 指向的对象的内容相同）。通常称 o’ 为 o 的克隆或副本。

 

2，什么时候使用Clone？

      当需要修改对象属性，又不想影响原来的属性值，这时候就应该使用clone了。

 

3，  Java 对 clone 的支持

      万类之初的  Object  类有  clone()  方法：  
       protected native Object clone() throws CloneNotSupportedException;  
该方法是 protected 的，显然是留待被子类 override 的。该方法又是 native 的，必然做了  
与具体平台相关的底层工作。  
事实上，类 Object 的 clone() 方法首先会检查 this.getClass() 是否实现了 Cloneable 接口。  
Cloneable 只是一个标志接口而已，用来标志该类是否有克隆功能。  
public interface Cloneable {

}  
     如果 this.getClass() 没有实现 Cloneable 接口， clone() 就会抛 CloneNotSupportedException 返回。否则就会创建一个类型为 this.getClass() 的对象 other ，并将 this 各 field 的值赋值给 other 的对应 field ，然后返回 other 。  
     如此一来，我们要定义一个具有 Clone 功能的类就相当方便：  
     1.  在类的声明中加入“ implements Cloneable ”，标志该类有克隆功能；  
     2. Override 类 Object 的 clone() 方法，在该方法中调用 super.clone() ：

 

4，shallow clone  and deep clone

    Clone是如何完成的呢？Object在对某个对象实施Clone时对其是一无所知的，它仅仅是简单地执行域对域的copy，这就是Shallow Clone。Java Collection 类库中具体数据结构类（ ArrayList/LinkedList ， HashSet/TreeSet ， HashMap/TreeMap 等）都具有克隆功能，且都是 Shallow Clone。在有种情况下，这种shallow Clone就会问题，这个情况就是当，要clone的对象中的某个属性是一个引用， 这样克隆类就和原始类共享了一部分信息，对这个属性操作就会影响原始类，所以就需要deep clone.

 

deep clone的简单例子：

```
public class TestClone {

	/**
	 * @param args
	 */

	public static void main(String[] args) {
		try {
			new TestClone().cloneObject();
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void cloneObject() throws CloneNotSupportedException {
		Person p = new Person();
		Man man = new Man();
		man.setSalory("111123");
		p.setName("zhangfei");
		p.setMan(man);
		//man.setSalory("122335");//(1)
		Person pp = p.getClonePerson(p);
		man.setSalory("122335");//(2)
		pp.setName("aa");
		System.out.println("pp.getName()= " + pp.getName() + " pp.man.getSalory()= "+pp.getMan().getSalory());
		System.out.println("p.getName()=" + p.getName()+" p.man.getSalory()= "+p.getMan().getSalory());
	}
}

class Person implements Cloneable {
	private String name = "";
	private Man man;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}


	public Person getClonePerson(Person p) throws CloneNotSupportedException {
		Person pp = (Person) p.clone();
		return pp;
	}

	public Man getMan() {
		return man;
	}

	public void setMan(Man man) {
		this.man = man;
	}
	
	public Object clone() throws CloneNotSupportedException{
		Person p = (Person) super.clone();
		p.man = this.getMan().getCloneMan(this.getMan());
		return p;
	}
}

class Man implements Cloneable{
	private String salory = "";

	public String getSalory() {
		return salory;
	}

	public void setSalory(String salory) {
		this.salory = salory;
	} 
	public Man getCloneMan(Man man) throws CloneNotSupportedException{
		Man ma = (Man)man.clone();
		return ma;
	}
}
```

 将注释(1),(2)替换注释后，看看输出结果。程序写的很乱，只为测试deep clone。见谅