码迷,mamicode.com
首页 > 编程语言 > 详细

关于Java深clone 的例子学习

时间:2015-11-27 12:47:37      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

之前http://www.cnblogs.com/lhppom/p/4857702.html里有提到关于Java的深克隆的学习,深浅区别就是在于仅复制对象引用和复制对象引用所指向的对象,最近在看《Java核心技术卷1》时,看到其中一个克隆的例子,这里再做个补充,其实现克隆的方式就是将对象中除数值或基本类以外的域再进行克隆,然后将引用给到新克隆的对象中所对应的域;

技术分享
 1 public class Pet implements Cloneable{
 2     String name;
 3     
 4     public Pet(String name){
 5         this.name = name;
 6     }
 7     
 8     public void setName(String name){
 9         this.name = name;
10     }
11     
12     public String toString(){
13         return "its name is "+name;
14     }
15     
16     public Pet clone(){
17         try {
18             return (Pet) super.clone();
19         } catch (CloneNotSupportedException e) {
20             // TODO Auto-generated catch block
21             e.printStackTrace();
22         }
23         return null;
24     }
25     
26 }
Pet类
技术分享
 1 public class Man implements Cloneable{
 2     String name;
 3     int age;
 4     Pet pet;
 5 
 6     public Man(String name, int age, Pet pet) {
 7         this.name = name;
 8         this.age = age;
 9         this.pet = pet;
10     }
11 
12     public Man clone() {
13         Man m;
14         Pet p;
15         try {
16             m = (Man) super.clone();
17             p = (Pet) this.pet.clone();
18             m.pet = p;
19             return m;
20         } catch (CloneNotSupportedException e) {
21             // TODO Auto-generated catch block
22             e.printStackTrace();
23         }
24         return null;
25     }
26 
27     public String toString() {
28         return "the man named " + name + " who is " + age
29                 + " years old owns the pet which " + pet.toString();
30     }
31 }
Man类
技术分享
public class Woman implements Cloneable {

    String name;
    int age;
    Pet pet;

    public Woman(String name, int age, Pet pet) {
        this.name = name;
        this.age = age;
        this.pet = pet;
    }

    public Object clone() {
        Object o;
        try {
            o = super.clone();
            return o;
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public String toString() {
        return "the woman named " + name + " who is " + age
                + " years old owns the pet which " + pet.toString();
    }
}
Woman类
技术分享
 1 public class CloneCompareTest {
 2   
 3     public static void main(String[] args) {
 4         Pet p = new Pet("jack");
 5         Woman w1 = new Woman("marry",21,p);
 6         Woman w2 = (Woman)w1.clone();
 7         w2.pet.setName("tom");
 8         System.out.println("w1: "+w1.toString());
 9         System.out.println("w2: "+w2.toString());
10         
11         Pet p1 = new Pet("michal");
12         Man m1= new Man("jack",32,p1);
13         Man m2 = m1.clone();
14         m2.pet.setName("gary");
15         System.out.println("m1: "+m1.toString());
16         System.out.println("m2: "+m2.toString());
17     }
18     
19 }
深浅克隆的异同测试类

在测试结果中,我们可以看到的确是实现了深克隆的效果:

w1: the woman named marry who is 21 years old owns the pet which its name is tom
w2: the woman named marry who is 21 years old owns the pet which its name is tom
m1: the man named jack who is 32 years old owns the pet which its name is michal
m2: the man named jack who is 32 years old owns the pet which its name is gary

关于Java深clone 的例子学习

标签:

原文地址:http://www.cnblogs.com/lhppom/p/5000012.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!