java Clone之深浅拷贝

要点:

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

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

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

看例子:

 1 /**
 2  * 浅度clone的对象(浅拷贝)
 3  *
 4  * @author yzl
 5  */
 6 public class ShallowCloneFavorite {
 7     private String name;
 8     public String getName() {
 9         return name;
10     }
11     public void setName(String name) {
12         this.name = name;
13     }
14 }

 

 1 /**
 2  * 深度clone(深拷贝)
 3  *
 4  * @author yzl
 5  */
 6 public class DepthCloneFavorite implements Cloneable{
 7     private String name;
 8 
 9     public String getName() {
10         return name;
11     }
12 
13     public void setName(String name) {
14         this.name = name;
15     }
16 
17     /* (non-Javadoc)
18      * @see java.lang.Object#clone()
19      */
20     @Override
21     protected DepthCloneFavorite clone() throws CloneNotSupportedException {
22         return (DepthCloneFavorite)super.clone();
23     }
24 }

 

聚合类:

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 
 4 /**
 5  * JAVA深浅Clone测试集成类
 6  *
 7  * @author yzl
 8  */
 9 public class Person implements Cloneable {
10     //基础类型直接值拷贝
11     private String type;
12     //未实现Cloneable接口的浅拷贝对象
13     private ShallowCloneFavorite scf;
14     //实现了Cloneable接口的可以进行深拷贝的对象
15     private DepthCloneFavorite dcf;
16     //可以深度Clone的list
17     private List<DepthCloneFavorite> dcfList;
18   
19     public String getType() {
20         return type;
21     }
22     public void setType(String type) {
23         this.type = type;
24     }
25     public ShallowCloneFavorite getScf() {
26         return scf;
27     }
28     public void setScf(ShallowCloneFavorite scf) {
29         this.scf = scf;
30     }
31     public DepthCloneFavorite getDcf() {
32         return dcf;
33     }
34     public void setDcf(DepthCloneFavorite dcf) {
35         this.dcf = dcf;
36     }
37 
38     public List<DepthCloneFavorite> getDcfList() {
39         return dcfList;
40     }
41     public void setDcfList(List<DepthCloneFavorite> dcfList) {
42         this.dcfList = dcfList;
43     }
44     
45     @Override
46     protected Person clone() throws CloneNotSupportedException {
47         Person p = (Person)super.clone();
48         //深拷贝Person的成员变量
49         p.setDcf(dcf.clone());
50         //深拷贝list
51         if(null!=dcfList && !dcfList.isEmpty()){
52             List<DepthCloneFavorite> copyList = new ArrayList<DepthCloneFavorite>(dcfList.size());
53             for(DepthCloneFavorite obj : dcfList){
54                 copyList.add(obj.clone());
55             }
56             p.setDcfList(copyList);
57         }
58         return p;
59     }
60     
61     /**
62      * 
63      * 显示对象里的数据
64      *
65      * @see [相关类/方法](可选)
66      * @since [产品/模块版本](可选)
67      */
68     public void display(){
69         System.out.println("------------------");
70         System.out.println("type: " + type);
71         System.out.println("ShallowCloneFavorite.name: " + this.scf.getName());
72         System.out.println("DepthCloneFavorite.name: " + dcf.getName());
73         if(null!=dcfList && !dcfList.isEmpty()){
74             for(int i=0; i<dcfList.size(); i++){
75                 System.out.println("DepthCloneFavoriteList["+ (i+1) +"].name: " + dcfList.get(i).getName());
76             }
77         }
78         System.out.println("------------------");
79     }
80 }

 

测试类:

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 
 4 /**
 5  * JAVA clone之深浅拷贝测试
 6  *
 7  * @author yzl
 8  */
 9 public class Test {
10     public static void main(String[] args) throws Exception {
11         Person p = getBasePerson();
12         p.display();
13         
14         Person p1 = p.clone();
15         p1.setType("girl");
16         p1.getDcf().setName("dancing");
17         p1.getScf().setName("singing");
18         p1.getDcfList().get(0).setName("yoga");
19         p1.display();
20         
21         p.display();
22     }
23     
24     private static Person getBasePerson(){
25         ShallowCloneFavorite scf = new ShallowCloneFavorite();
26         scf.setName("basketball");
27         
28         DepthCloneFavorite dcf = new DepthCloneFavorite();
29         dcf.setName("football");
30         
31         DepthCloneFavorite dcf2 = new DepthCloneFavorite();
32         dcf2.setName("baseball");
33         List<DepthCloneFavorite> dcfList = new ArrayList<DepthCloneFavorite>(1);
34         dcfList.add(dcf2);
35         
36         Person p = new Person();
37         p.setType("boy");
38         p.setDcf(dcf);
39         p.setDcfList(dcfList);
40         p.setScf(scf);
41         
42         return p;
43     }
44 }

运行结果如下:

 1 ------------------
 2 type: boy
 3 ShallowCloneFavorite.name: basketball(浅度拷贝,值变化了)
 4 DepthCloneFavorite.name: football
 5 DepthCloneFavoriteList[1].name: baseball
 6 ------------------
 7 ------------------
 8 type: girl
 9 ShallowCloneFavorite.name: singing
10 DepthCloneFavorite.name: dancing
11 DepthCloneFavoriteList[1].name: yoga
12 ------------------
13 ------------------
14 type: boy
15 ShallowCloneFavorite.name: singing(浅度拷贝,值变化了)
16 DepthCloneFavorite.name: football
17 DepthCloneFavoriteList[1].name: baseball
18 ------------------

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值