-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusic5.java
97 lines (80 loc) · 1.92 KB
/
Music5.java
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
enum Note {
CSHARP,
DSHARP,
BFLAT
}
interface Instrument {
// Compile-time constant:
int VALUE = 5; // static & final
// Cannot have method definitions:
void play(Note n); // Automatically public
void adjust();
}
class Wind implements Instrument {
public void play(Note n) {
System.out.println(this + ".play() " + n);
}
public String toString() {
return "Wind";
}
public void adjust() {
System.out.println(this + ".adjust()");
}
}
class Percussion implements Instrument {
public void play(Note n) {
System.out.println(this + ".play() " + n);
}
public String toString() {
return "Percussion";
}
public void adjust() {
System.out.println(this + ".adjust()");
}
}
class Stringed implements Instrument {
public void play(Note n) {
System.out.println(this + ".play() " + n);
}
public String toString() {
return "Stringed";
}
public void adjust() {
System.out.println(this + ".adjust()");
}
}
class Brass extends Wind {
public String toString() {
return "Brass";
}
}
class Woodwind extends Wind {
public String toString() {
return "Woodwind";
}
}
public class Music5 {
// Doesn’t care about type, so new types
// added to the system still work right:
static void tune(Instrument i) {
// ...
i.play(Note.BFLAT);
}
static void tuneAll(Instrument[] e) {
for (Instrument i : e)
tune(i);
}
public static void main(String[] args) {
// Upcasting during addition to the array:
Instrument[] orchestra = {
new Wind(),
new Percussion(),
new Stringed(),
new Brass(),
new Woodwind()
};
tuneAll(orchestra);
Note b = Note.BFLAT;
System.out.println(b.ordinal());
}
}