-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample126.java
More file actions
51 lines (40 loc) · 1.2 KB
/
Example126.java
File metadata and controls
51 lines (40 loc) · 1.2 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
// Example 126 from page 95
//
import java.awt.Color;
class Example126 {
public static void main(String[] args) {
LabelPoint<String> p1 = new LabelPoint<String>(5, 117, "home"),
p2 = new LabelPoint<String>(2, 3, "work");
LabelPoint<Double> p3 = new LabelPoint<Double>(10, 100, 3.1415);
ColorLabelPoint<String,Integer> p4 =
new ColorLabelPoint<String,Integer>(20, 30, "foo", 0x0000FF);
ColorLabelPoint<String,Color> p5 =
new ColorLabelPoint<String,Color>(40, 50, "bar", Color.BLUE);
Movable[] movables = { p1, p2, p3, p4, p5 };
LabelPoint<String>[] stringpoints; // Legal
// stringpoints = new LabelPoint<String>[3]; // Illegal
}
}
interface Movable {
void move(int dx, int dy);
}
class LabelPoint<L> implements Movable {
protected int x, y;
private L lab;
public LabelPoint(int x, int y, L lab) {
this.x = x; this.y = y; this.lab = lab;
}
public void move(int dx, int dy) {
x += dx; y += dy;
}
public L getLab() {
return lab;
}
}
class ColorLabelPoint<L, C> extends LabelPoint<L> {
private C c;
public ColorLabelPoint(int x, int y, L lab, C c) {
super(x, y, lab);
this.c = c;
}
}