|
3 | 3 | // We make no guarantees that this code is fit for any purpose. |
4 | 4 | // Visit http://OnJava8.com for more book information. |
5 | 5 |
|
6 | | -interface HasColor { java.awt.Color getColor(); } |
| 6 | +interface HasColor { |
| 7 | + java.awt.Color getColor(); |
| 8 | +} |
7 | 9 |
|
8 | 10 | class WithColor<T extends HasColor> { |
9 | | - T item; |
10 | | - WithColor(T item) { this.item = item; } |
11 | | - T getItem() { return item; } |
12 | | - // The bound allows you to call a method: |
13 | | - java.awt.Color color() { return item.getColor(); } |
| 11 | + T item; |
| 12 | + |
| 13 | + WithColor(T item) { |
| 14 | + this.item = item; |
| 15 | + } |
| 16 | + |
| 17 | + T getItem() { |
| 18 | + return item; |
| 19 | + } |
| 20 | + |
| 21 | + // The bound allows you to call a method: |
| 22 | + java.awt.Color color() { |
| 23 | + return item.getColor(); |
| 24 | + } |
14 | 25 | } |
15 | 26 |
|
16 | | -class Coord { public int x, y, z; } |
| 27 | +class Coord { |
| 28 | + public int x, y, z; |
| 29 | +} |
17 | 30 |
|
18 | 31 | // This fails. Class must be first, then interfaces: |
19 | 32 | // class WithColorCoord<T extends HasColor & Coord> { |
20 | 33 |
|
21 | 34 | // Multiple bounds: |
22 | 35 | class WithColorCoord<T extends Coord & HasColor> { |
23 | | - T item; |
24 | | - WithColorCoord(T item) { this.item = item; } |
25 | | - T getItem() { return item; } |
26 | | - java.awt.Color color() { return item.getColor(); } |
27 | | - int getX() { return item.x; } |
28 | | - int getY() { return item.y; } |
29 | | - int getZ() { return item.z; } |
| 36 | + T item; |
| 37 | + |
| 38 | + WithColorCoord(T item) { |
| 39 | + this.item = item; |
| 40 | + } |
| 41 | + |
| 42 | + T getItem() { |
| 43 | + return item; |
| 44 | + } |
| 45 | + |
| 46 | + java.awt.Color color() { |
| 47 | + return item.getColor(); |
| 48 | + } |
| 49 | + |
| 50 | + int getX() { |
| 51 | + return item.x; |
| 52 | + } |
| 53 | + |
| 54 | + int getY() { |
| 55 | + return item.y; |
| 56 | + } |
| 57 | + |
| 58 | + int getZ() { |
| 59 | + return item.z; |
| 60 | + } |
30 | 61 | } |
31 | 62 |
|
32 | | -interface Weight { int weight(); } |
| 63 | +interface Weight { |
| 64 | + int weight(); |
| 65 | +} |
33 | 66 |
|
34 | 67 | // As with inheritance, you can have only one |
35 | 68 | // concrete class but multiple interfaces: |
36 | 69 | class Solid<T extends Coord & HasColor & Weight> { |
37 | | - T item; |
38 | | - Solid(T item) { this.item = item; } |
39 | | - T getItem() { return item; } |
40 | | - java.awt.Color color() { return item.getColor(); } |
41 | | - int getX() { return item.x; } |
42 | | - int getY() { return item.y; } |
43 | | - int getZ() { return item.z; } |
44 | | - int weight() { return item.weight(); } |
| 70 | + T item; |
| 71 | + |
| 72 | + Solid(T item) { |
| 73 | + this.item = item; |
| 74 | + } |
| 75 | + |
| 76 | + T getItem() { |
| 77 | + return item; |
| 78 | + } |
| 79 | + |
| 80 | + java.awt.Color color() { |
| 81 | + return item.getColor(); |
| 82 | + } |
| 83 | + |
| 84 | + int getX() { |
| 85 | + return item.x; |
| 86 | + } |
| 87 | + |
| 88 | + int getY() { |
| 89 | + return item.y; |
| 90 | + } |
| 91 | + |
| 92 | + int getZ() { |
| 93 | + return item.z; |
| 94 | + } |
| 95 | + |
| 96 | + int weight() { |
| 97 | + return item.weight(); |
| 98 | + } |
45 | 99 | } |
46 | 100 |
|
47 | 101 | class Bounded |
48 | | -extends Coord implements HasColor, Weight { |
49 | | - @Override |
50 | | - public java.awt.Color getColor() { return null; } |
51 | | - @Override public int weight() { return 0; } |
| 102 | + extends Coord implements HasColor, Weight { |
| 103 | + @Override |
| 104 | + public java.awt.Color getColor() { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public int weight() { |
| 110 | + return 0; |
| 111 | + } |
52 | 112 | } |
53 | 113 |
|
54 | 114 | public class BasicBounds { |
55 | | - public static void main(String[] args) { |
56 | | - Solid<Bounded> solid = |
57 | | - new Solid<>(new Bounded()); |
58 | | - solid.color(); |
59 | | - solid.getY(); |
60 | | - solid.weight(); |
61 | | - } |
| 115 | + public static void main(String[] args) { |
| 116 | + Solid<Bounded> solid = |
| 117 | + new Solid<>(new Bounded()); |
| 118 | + solid.color(); |
| 119 | + solid.getY(); |
| 120 | + solid.weight(); |
| 121 | + } |
62 | 122 | } |
0 commit comments