-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloneTest.java
More file actions
41 lines (36 loc) · 1.62 KB
/
CloneTest.java
File metadata and controls
41 lines (36 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
package cloneTest;
import java.io.IOException;
import java.util.Date;
/*
* clone()方法必须实现Clonable接口,否则会出现CloneNotSupportedException异常
*
* */
public class CloneTest {
public static void main(String[] args) {
Wife wife=new Wife("张三",new Date());
Husband husband=new Husband(wife, 30);
/*浅克隆:只克隆了外层对象,对象中的对象并没有克隆*/
Husband husband2=(Husband) husband.clone();
husband2.setAge(40);
husband2.getWife().setName("李四");
System.out.println("husband1 age:"+husband.getAge()+" husband2 age:"+husband2.getAge());
System.out.println("husband1和huaband2是否是同一对象:"+(husband==husband2));
System.out.println("wife name:"+husband.getWife().getName()+" wife2 name:"+husband2.getWife().getName());
System.out.println("wife是否是同一个对象:"+(husband.getWife()==husband2.getWife()));
System.out.println();
try {
/*深克隆:实现序列化接口,将对象写入流中。
* */
Husband husband3=(Husband) husband.deepClone();
husband3.setAge(50);
husband3.getWife().setName("李四");
System.out.println("husband1 age:"+husband.getAge()+" husband3 age:"+husband3.getAge());
System.out.println("wife name:"+husband.getWife().getName()+" wife3 name:"+husband3.getWife().getName());
System.out.println("husband1和huaband3是否是同一对象:"+(husband==husband3));
System.out.println("wife是否是同一个对象:"+(husband.getWife()==husband3.getWife()));
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}