-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTicketCloneable.java
More file actions
71 lines (59 loc) · 1.91 KB
/
Copy pathTicketCloneable.java
File metadata and controls
71 lines (59 loc) · 1.91 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
package prototype;
import java.io.*;
/**
* @author 1754025徐菡志
*/
public abstract class TicketCloneable implements Cloneable, Serializable {
protected double price;
protected String date;
protected String type;
public Ticket giftticket;//complimentary ticket object
/**
* Make a shallow copy of itself.
* @return ticket: the shallow copy of the object.
*/
@Override
public Object clone(){
Ticket ticket = null;
try {
ticket = (Ticket)super.clone();
} catch (Exception e){
System.out.println(e.getMessage());
}
System.out.println(getClass().getSimpleName()+":("+this.hashCode()+"):clone():make a shallow copy of "+ this.getInfo());
return ticket;
}
/**
* Make a deep copy of itself.
* @return ticket: the deep copy of the object.
*/
public Object deepClone(){
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try{
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(this);
bis = new ByteArrayInputStream(bos.toByteArray());
ois = new ObjectInputStream(bis);
Ticket copyticket = (Ticket)ois.readObject();
System.out.println(getClass().getSimpleName()+":("+ this.hashCode()+"):deepclone():make a deep copy of "+ this.getInfo());
return copyticket;
} catch (Exception e){
e.printStackTrace();
return null;
}finally {
try {
bos.close();
oos.close();
bis.close();
ois.close();
}catch (Exception e2){
System.out.println(e2.getMessage());
}
}
}
public abstract String getInfo();
}