Skip to content

Commit d402018

Browse files
committed
java/se: add object#clone sample
1 parent 7ad9f83 commit d402018

File tree

2 files changed

+41
-25
lines changed
  • java/javase
    • javase-common/src/main/java/cn/edu/ntu/java/javase/common/model
    • javase-syntax/src/main/java/cn/edu/ntu/java/javase/syntax/object

2 files changed

+41
-25
lines changed

java/javase/javase-common/src/main/java/cn/edu/ntu/java/javase/common/model/Employee.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ public class Employee implements Serializable {
1212
private double salary;
1313
private Address address;
1414

15+
/*
16+
@Override
17+
protected Object clone() throws CloneNotSupportedException {
18+
Employee clone = (Employee) super.clone();
19+
clone.address = (Address) clone.address.clone();
20+
21+
return clone;
22+
}
23+
*/
24+
1525
public Employee(String name, int age, double salary, Address address) {
1626
this.name = name;
1727
this.age = age;

java/javase/javase-syntax/src/main/java/cn/edu/ntu/java/javase/syntax/object/CloneTest.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,6 @@ public class CloneTest {
2323
// *******************************************
2424
// 浅克隆
2525
// *******************************************
26-
27-
public static <T> T deepClone(T object) {
28-
try {
29-
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
30-
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
31-
objectOutputStream.writeObject(object);
32-
ByteArrayInputStream bais =
33-
new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
34-
ObjectInputStream objectInputStream = new ObjectInputStream(bais);
35-
return (T) objectInputStream.readObject();
36-
} catch (Exception e) {
37-
e.printStackTrace();
38-
return null;
39-
}
40-
}
41-
4226
@Test
4327
public void testShadowClone4Map() {
4428
HashMap<String, Address> map = new HashMap<>();
@@ -49,10 +33,6 @@ public void testShadowClone4Map() {
4933
Assert.isTrue(clone.get("nanjing") == map.get("nanjing"));
5034
}
5135

52-
// *******************************************
53-
// 深克隆
54-
// *******************************************
55-
5636
@Test
5737
public void testShadowClone4List() {
5838
ArrayList<Address> list = new ArrayList<>();
@@ -63,6 +43,23 @@ public void testShadowClone4List() {
6343
Assert.isTrue(clone.get(0) == list.get(0));
6444
}
6545

46+
// *******************************************
47+
// 深克隆1: 序列化(小推荐)
48+
// *******************************************
49+
public static <T> T deepClone(T object) {
50+
try {
51+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
52+
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
53+
objectOutputStream.writeObject(object);
54+
ByteArrayInputStream bais =
55+
new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
56+
ObjectInputStream objectInputStream = new ObjectInputStream(bais);
57+
return (T) objectInputStream.readObject();
58+
} catch (Exception e) {
59+
e.printStackTrace();
60+
return null;
61+
}
62+
}
6663
/** 需要 Address & Employee 实现序列化接口 */
6764
@Test
6865
public void whenModifyingOriginalObject_thenSerializableShouldNotChange() {
@@ -76,6 +73,9 @@ public void whenModifyingOriginalObject_thenSerializableShouldNotChange() {
7673
Assert.isFalse(deepCopy.getAddress().getCity().equals(pm.getAddress().getCity()));
7774
}
7875

76+
// *******************************************
77+
// 深克隆2: json(obj-str-obj)(小推荐)
78+
// *******************************************
7979
/** 不需要 Address & Employee 实现序列化接口 */
8080
@Test
8181
public void whenModifyingOriginalObject_thenGsonCloneShouldNotChange() {
@@ -89,11 +89,7 @@ public void whenModifyingOriginalObject_thenGsonCloneShouldNotChange() {
8989
Assert.isFalse(deepCopy.getAddress().getCity().equals(pm.getAddress().getCity()));
9090
}
9191

92-
/**
93-
* 不需要 Address & Employee 实现序列化接口
94-
*
95-
* @throws IOException
96-
*/
92+
/** 不需要 Address & Employee 实现序列化接口 */
9793
@Test
9894
public void whenModifyingOriginalObject_thenJacksonCopyShouldNotChange() throws IOException {
9995
Address address = new Address("xuzhou", "jiangsu");
@@ -104,4 +100,14 @@ public void whenModifyingOriginalObject_thenJacksonCopyShouldNotChange() throws
104100
address.setCity("shanghai");
105101
Assert.isFalse(deepCopy.getAddress().getCity().equals(pm.getAddress().getCity()));
106102
}
103+
104+
// *******************************************
105+
// 深克隆3: 递归 clone(小推荐), 逻辑上判断后复制
106+
// *******************************************
107+
108+
// *******************************************
109+
// 深克隆4: 重写最外层对象的 clone(不推荐)
110+
// @see Employee#clone()
111+
// *******************************************
112+
107113
}

0 commit comments

Comments
 (0)