-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCloneTest3.java
More file actions
92 lines (76 loc) · 1.85 KB
/
CloneTest3.java
File metadata and controls
92 lines (76 loc) · 1.85 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
package clone;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* ʹÓÃÐòÁл¯ÊµÏÖÉ±´
* @author vonzhou
*
*/
public class CloneTest3 {
public static void main(String[] args) throws Exception {
Address2 addr=new Address2("shiyan", 442500);
Customer2 c=new Customer2("vonzhou", addr);
Customer2 c2=(Customer2)c.deepCopy();
System.out.println(c2.getName()+c2.getAddress());
addr=new Address2("wuhan", 430065);
c.setAddress(addr);
System.out.println(c.getName()+c.getAddress());
System.out.println(c2.getName()+c2.getAddress());
}
}
class Customer2 implements Serializable{
private String name;
private Address2 address;
public Customer2(String name, Address2 address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address2 getAddress() {
return address;
}
public void setAddress(Address2 address) {
this.address = address;
}
public Object deepCopy() throws Exception{
ByteArrayOutputStream bos=new ByteArrayOutputStream();
//
ObjectOutputStream oos=new ObjectOutputStream(bos);
oos.writeObject(this);
ObjectInputStream ois=new ObjectInputStream(
new ByteArrayInputStream(bos.toByteArray()));
return ois.readObject();
}
}
class Address2 implements Serializable{
private String loc;
private int code;
public Address2(String loc, int code) {
this.loc = loc;
this.code = code;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
@Override
public String toString() {
return " ADDRESS: "+this.loc+" "+this.code;
}
}