java Clone之深浅拷贝

要点:

1、浅度拷贝可以不实现Cloneable接口(自动使用Object.clone)或者不重写Cloneable的clone方法。

2、要被深度拷贝的类必须实现Cloneable接口并重写clone方法。

3、如果需要能被深度拷贝则需要在父一级对所有的非基本类型的子元素调用clone方法。

看例子:

 /**
* 浅度clone的对象(浅拷贝)
*
* @author yzl
*/
public class ShallowCloneFavorite {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
 /**
* 深度clone(深拷贝)
*
* @author yzl
*/
public class DepthCloneFavorite implements Cloneable{
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} /* (non-Javadoc)
* @see java.lang.Object#clone()
*/
@Override
protected DepthCloneFavorite clone() throws CloneNotSupportedException {
return (DepthCloneFavorite)super.clone();
}
}

聚合类:

 import java.util.ArrayList;
import java.util.List; /**
* JAVA深浅Clone测试集成类
*
* @author yzl
*/
public class Person implements Cloneable {
//基础类型直接值拷贝
private String type;
//未实现Cloneable接口的浅拷贝对象
private ShallowCloneFavorite scf;
//实现了Cloneable接口的可以进行深拷贝的对象
private DepthCloneFavorite dcf;
//可以深度Clone的list
private List<DepthCloneFavorite> dcfList; public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public ShallowCloneFavorite getScf() {
return scf;
}
public void setScf(ShallowCloneFavorite scf) {
this.scf = scf;
}
public DepthCloneFavorite getDcf() {
return dcf;
}
public void setDcf(DepthCloneFavorite dcf) {
this.dcf = dcf;
} public List<DepthCloneFavorite> getDcfList() {
return dcfList;
}
public void setDcfList(List<DepthCloneFavorite> dcfList) {
this.dcfList = dcfList;
} @Override
protected Person clone() throws CloneNotSupportedException {
Person p = (Person)super.clone();
//深拷贝Person的成员变量
p.setDcf(dcf.clone());
//深拷贝list
if(null!=dcfList && !dcfList.isEmpty()){
List<DepthCloneFavorite> copyList = new ArrayList<DepthCloneFavorite>(dcfList.size());
for(DepthCloneFavorite obj : dcfList){
copyList.add(obj.clone());
}
p.setDcfList(copyList);
}
return p;
} /**
*
* 显示对象里的数据
*
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public void display(){
System.out.println("------------------");
System.out.println("type: " + type);
System.out.println("ShallowCloneFavorite.name: " + this.scf.getName());
System.out.println("DepthCloneFavorite.name: " + dcf.getName());
if(null!=dcfList && !dcfList.isEmpty()){
for(int i=0; i<dcfList.size(); i++){
System.out.println("DepthCloneFavoriteList["+ (i+1) +"].name: " + dcfList.get(i).getName());
}
}
System.out.println("------------------");
}
}

测试类:

 import java.util.ArrayList;
import java.util.List; /**
* JAVA clone之深浅拷贝测试
*
* @author yzl
*/
public class Test {
public static void main(String[] args) throws Exception {
Person p = getBasePerson();
p.display(); Person p1 = p.clone();
p1.setType("girl");
p1.getDcf().setName("dancing");
p1.getScf().setName("singing");
p1.getDcfList().get(0).setName("yoga");
p1.display(); p.display();
} private static Person getBasePerson(){
ShallowCloneFavorite scf = new ShallowCloneFavorite();
scf.setName("basketball"); DepthCloneFavorite dcf = new DepthCloneFavorite();
dcf.setName("football"); DepthCloneFavorite dcf2 = new DepthCloneFavorite();
dcf2.setName("baseball");
List<DepthCloneFavorite> dcfList = new ArrayList<DepthCloneFavorite>(1);
dcfList.add(dcf2); Person p = new Person();
p.setType("boy");
p.setDcf(dcf);
p.setDcfList(dcfList);
p.setScf(scf); return p;
}
}

运行结果如下:

 ------------------
type: boy
ShallowCloneFavorite.name: basketball(浅度拷贝,值变化了)
DepthCloneFavorite.name: football
DepthCloneFavoriteList[1].name: baseball
------------------
------------------
type: girl
ShallowCloneFavorite.name: singing
DepthCloneFavorite.name: dancing
DepthCloneFavoriteList[1].name: yoga
------------------
------------------
type: boy
ShallowCloneFavorite.name: singing(浅度拷贝,值变化了)
DepthCloneFavorite.name: football
DepthCloneFavoriteList[1].name: baseball
------------------
上一篇:eclipse编译项目:Java @Override 注解报错的解决方法


下一篇:国内第一部C#.Net调用Matlab混合编程视频教程