-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeepClone.java
More file actions
60 lines (53 loc) · 1.62 KB
/
DeepClone.java
File metadata and controls
60 lines (53 loc) · 1.62 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
package btp.oneP;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class DeepClone implements Serializable,Cloneable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private DeepClone son;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public DeepClone getSon() {
return son;
}
public void setSon(DeepClone son) {
this.son = son;
}
public DeepClone(String name) {
super();
this.name = name;
}
public DeepClone deepClone() throws IOException, ClassNotFoundException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return (DeepClone)ois.readObject();
}
@Override
public String toString() {
return "DeepClone [name=" + name + ", son=" + son + "]";
}
public static void main(String[] args) throws ClassNotFoundException, IOException, CloneNotSupportedException {
DeepClone f = new DeepClone("father");
f.setSon(new DeepClone("son"));
DeepClone clone = f.deepClone();
System.out.println("É±´£º"+clone);
System.out.println(clone.getSon() == f.getSon());
DeepClone ff = (DeepClone)f.clone();
System.out.println("dz¿½±´£º"+ff);
System.out.println(ff.getSon() == f.getSon());
}
}