-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHusband.java
More file actions
58 lines (52 loc) · 1.27 KB
/
Husband.java
File metadata and controls
58 lines (52 loc) · 1.27 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
package cloneTest;
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 Husband implements Cloneable,Serializable{
private Wife wife;
private int age;
public Husband(Wife wife, int age) {
super();
this.wife = wife;
this.age = age;
}
public Wife getWife() {
return wife;
}
public int getAge() {
return age;
}
public void setWife(Wife wife) {
this.wife = wife;
}
public void setAge(int age) {
this.age = age;
}
//浅克隆
public Object clone(){
Husband husband=null;
try {
husband=(Husband) super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}finally{
return husband;
}
}
//深克隆
public Object deepClone() throws IOException, ClassNotFoundException{
//将对象写到流中
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream obs=new ObjectOutputStream(baos);
obs.writeObject(this);
//立即将对象取出
ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois=new ObjectInputStream(bais);
return ois.readObject();
}
}