对象拷贝-深拷贝

 1 package com.yj.fenghao.clone;
 2 //深拷贝的简单测试
 3 import java.util.Date;
 4 
 5 public class Employee implements Cloneable{
 6     
 7     private String name;
 8     private String age;
 9     private String salary;
10     private String career;
11     private Date date;
12     public String getName() {
13         return name;
14     }
15     public void setName(String name) {
16         this.name = name;
17     }
18     public String getAge() {
19         return age;
20     }
21     public void setAge(String age) {
22         this.age = age;
23     }
24     public String getSalary() {
25         return salary;
26     }
27     public void setSalary(String salary) {
28         this.salary = salary;
29     }
30     public String getCareer() {
31         return career;
32     }
33     public void setCareer(String career) {
34         this.career = career;
35     }
36     public Date getDate() {
37         return date;
38     }
39     public void setDate(Date date) {
40         this.date = date;
41     }
42     public Employee(String name, String age, String salary, String career, Date date) {
43         super();
44         this.name = name;
45         this.age = age;
46         this.salary = salary;
47         this.career = career;
48         this.date = date;
49     }
50     public Employee() {
51         super();
52     }
53 //    /**
54 //     * 如果对象中含有其他的对象,调用超类的克隆方法是浅克隆
55 //     */
56 //    @Override
57 //    protected Object clone() throws CloneNotSupportedException {
58 //        return super.clone();
59 //    }
60     
61     /**
62      * 如果对象中含有其他的对象,其他的对象也需要克隆,即使深拷贝
63      */
64     @Override
65     protected Employee clone() throws CloneNotSupportedException {
66         Employee emply = (Employee)super.clone();
67         emply.date= (Date)date.clone();
68         return emply;
69     }
70     @Override
71     public String toString() {
72         return "{\"name\":"+name+",\"age\":"+age+",\"salary\":"+salary+",\"career\":"+career+",\"date\":+"+date+"}";
73     }
74     
75 
76 }
 1 package com.yj.fenghao.clone;
 2 
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 
 7 
 8 
 9 public class CloneableTest {
10     
11     
12     public static void main(String[] args) throws CloneNotSupportedException, ParseException {
13         SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
14         Employee em=new Employee();
15         em.setDate(new Date());
16         em.setName("fenghao");
17         em.setAge("25");
18         em.setSalary("1000");
19         em.setCareer("111");
20         Employee clone = em.clone();
21         clone.setDate(format.parse("2015-01-01 12:00:00"));
22         clone.setName("xuxiaoxiao");
23         System.out.println(em.toString());
24         System.out.println(clone.toString());
25     }
26 
27 }

运行结果:

{"name":fenghao,"age":25,"salary":1000,"career":111,"date":+Fri Apr 07 08:29:29 CST 2017}
{"name":xuxiaoxiao,"age":25,"salary":1000,"career":111,"date":+Thu Jan 01 12:00:00 CST 2015}

 

posted @ 2017-04-07 08:34  默默行走  阅读(169)  评论(0编辑  收藏  举报