-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprototype.java
More file actions
63 lines (51 loc) · 1.23 KB
/
prototype.java
File metadata and controls
63 lines (51 loc) · 1.23 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
interface Prototype {
Object clone();
}
class Packet implements Prototype {
public Packet(int src, int dest) {
this.src = src;
this.dest = dest;
}
private int src;
private int dest;
public int getSrc() {
return src;
}
public void setSrc(int src) {
this.src = src;
}
public int getDest() {
return dest;
}
public void setDest(int dest) {
this.dest = dest;
}
private Packet copy(){
return new Packet(src, dest);
}
@Override
public Object clone() {
return this.copy();
}
void show(){
System.out.printf("src: %d, dest: %d\n", src, dest);
}
}
public class Main {
public static void main(String[] args) {
Packet packet1 = new Packet(12, 30);
Packet packet2 = (Packet)packet1.clone();
Packet packet3 = (Packet)packet1.clone();
Packet packet4 = (Packet)packet1.clone();
Packet packet5 = (Packet)packet1.clone();
packet2.setDest(40);
packet3.setDest(100);
packet4.setDest(11);
packet5.setDest(87);
packet1.show();
packet2.show();
packet3.show();
packet4.show();
packet5.show();
}
}