-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCloneTest2.java
More file actions
67 lines (64 loc) · 1.52 KB
/
CloneTest2.java
File metadata and controls
67 lines (64 loc) · 1.52 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
package clone;
public class CloneTest2 {
public static void main(String[] args) throws Exception {
Address addr=new Address("shiyan", 442500);
Customer c=new Customer("vonzhou", addr);
Customer c2=(Customer)c.clone();
System.out.println(c2.getName()+c2.getAddress());
addr=new Address("wuhan", 430065);
c.setAddress(addr);
System.out.println(c.getName()+c.getAddress());
System.out.println(c2.getName()+c2.getAddress());
}
}
class Customer implements Cloneable{
private String name;
private Address address;
public Customer(String name, Address address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Object clone() throws CloneNotSupportedException {
Customer c=(Customer)super.clone();
c.setAddress((Address)address.clone());//ʵÏÖÉ±´deep clone
return c;
}
}
class Address implements Cloneable{
private String loc;
private int code;
public Address(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;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
public String toString() {
return " ADDRESS: "+this.loc+" "+this.code;
}
}