-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCopyT.java
More file actions
139 lines (121 loc) · 3.42 KB
/
CopyT.java
File metadata and controls
139 lines (121 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* <html>
* <body>
* <P> Copyright 1994 JsonInternational</p>
* <p> All rights reserved.</p>
* <p> Created by Jason https://github.com/Jasonandy/patterns</p>
* </body>
* </html>
*/
package cn.ucaner.core.base.copy;
/*
* Copyright [2015] [Jeff Lee]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @Package:cn.ucaner.core.base.copy
* @ClassName:Family
* @Description: <p> 深拷贝与浅拷贝
* {@link https://blog.csdn.net/XIAXIA__/article/details/41652057}
* {@link https://blog.csdn.net/u014727260/article/details/55003402}
* {@link https://blog.csdn.net/baiye_xing/article/details/71788741}
* </p>
* @Author: - Jeff Lee
* @CreatTime:2018年4月5日 下午1:40:35
* @Modify By:
* @ModifyTime: 2018年4月5日
* @Modify marker:
* @version V1.0
*/
class Family implements Cloneable{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* 深拷贝
* @return
*/
/*@Override
public Object clone() {
Object o = null;
try {
o = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}*/
}
class Student implements Cloneable{
private String name;
private Family family;//Family Cloneable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Family getFamily() {
return family;
}
public void setFamily(Family family) {
this.family = family;
}
/**
* 浅拷贝 对其对象的引用却没有拷贝
* @return
* @throws CloneNotSupportedException
*/
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
/**
* 深拷贝
*/
/*@Override
protected Object clone() {
Student o = null;
try {
o = (Student)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
o.family = (Family) family.clone();
return o;
}*/
}
public class CopyT {
public static void main(String[] args) throws CloneNotSupportedException {
Family family = new Family();
family.setName("Jeff Family obj ");
Student student1 = new Student();
student1.setFamily(family);
student1.setName("Jeff String");
Student student2 = (Student) student1.clone();//
student2.setName("Jeff2");
student2.getFamily().setName("Jeff2 Family");
System.out.println(student1.getName() + " " + student1.getFamily().getName());
System.out.println(student2.getName() + " " + student2.getFamily().getName());
/* Jeff Jeff2 Family
Jeff2 Jeff2 Family*/
}
}
//Outputs
//Jeff String Jeff2 Family
//Jeff2 Jeff2 Family