-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCloneTest1.java
More file actions
39 lines (35 loc) · 869 Bytes
/
CloneTest1.java
File metadata and controls
39 lines (35 loc) · 869 Bytes
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
package clone;
public class CloneTest1 {
public static void main(String[] args) throws Exception {
Person p=new Person();
p.setAge(21);
p.setName("vonzhou");
Person p2=(Person)p.clone();
System.out.println(p2.getName()+" : "+p2.getAge());
p2.setName("fengzhou");//浅拷贝,name指向了新的String对象
p2.setAge(10);
System.out.println("==========================");
System.out.println(p.getName()+" : "+p.getAge());
System.out.println(p2.getName()+" : "+p2.getAge());
}
}
class Person implements Cloneable{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}