-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMusic4.java
More file actions
118 lines (89 loc) · 1.69 KB
/
Copy pathMusic4.java
File metadata and controls
118 lines (89 loc) · 1.69 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package unit9;
import unit8.music.Note;
abstract class Instrument {
private int i;
public abstract void play(Note n);
public String what() {
return "Instrument";
}
public abstract void adjust();
}
class Wind extends Instrument {
@Override
public void play(Note n) {
System.out.println("Wind.play() " + n);
}
@Override
public String what() {
return "Wind";
}
@Override
public void adjust() {
}
}
class Percussion extends Instrument {
@Override
public void play(Note n) {
System.out.println("Percussion.play() " + n);
}
@Override
public String what() {
return "Percussion";
}
@Override
public void adjust() {
}
}
class Stringed extends Instrument {
@Override
public void play(Note n) {
System.out.println("Stringed.play() " + n);
}
@Override
public String what() {
return "Stringed";
}
@Override
public void adjust() {
}
}
class Brass extends Wind {
public void play(Note n) {
System.out.println("Brass.play() " + n);
}
public void adjust() {
System.out.println("Brass.adjust()");
}
}
class WoodWind extends Wind {
public void play(Note n) {
System.out.println("WoodWind.play() " + n);
}
@Override
public String what() {
return "WoodWind";
}
}
public class Music4 {
// 多态的运用
static void tune(Instrument i) {
i.play(Note.MIDDLE_C);
}
static void tuneAll(Instrument[] i) {
for (Instrument instrument : i) {
tune(instrument);
}
}
public static void main(String[] args) {
Instrument[] o = { new Wind(), new Percussion(), new Stringed(), new Brass(), new WoodWind() };
tuneAll(o);
}
}
/*
Wind.play() MIDDLE_C
Percussion.play() MIDDLE_C
Stringed.play() MIDDLE_C
Brass.play() MIDDLE_C
WoodWind.play() MIDDLE_C
gh
*/