//Note.java
package polymorphism.music;
public enum Note{
MIDDLE_C,C_SHARP,B_FALT;//Etc
}
//Music8
import java.util.*;
import static will.Print.*;
import polymorphism.music.Note;
class Instrument{
void play(Note n){print("Instrument.play() "+n);}
public String toString(){return "Instrument";}
void adjust(){print("Adjusting Instrument");}
}
class Wind extends Instrument{
void play(Note n){print("Wind.play() "+n);}
public String toString(){return "Wind";}
void adjust(){print("Adjusting Wind");}
}
class Percussion extends Instrument{
void play(Note n){print("Percussion.play() "+n);}
public String toString(){return "Wind";}
void adjust(){print("Adjusting Percussion");}
}
class Stringed extends Instrument{
void play(Note n){print("Stringed.play() "+n);}
public String toString(){return "Wind";}
void adjust(){print("Adjusting Stringed");}
}
class Brass extends Wind{
void play(Note n){print("Brass.play() "+n);}
public String toString(){return "Wind";}
void adjust(){print("Adjusting Brass");}
}
class WoodWind extends Wind{
void play(Note n){print("WoodWind.play() "+n);}
public String toString(){return "Wind";}
void adjust(){print("Adjusting WoodWind");}
}
public class Music8{
private Random rand = new Random(23);
public Instrument tune(){
switch(rand.nextInt(3)){
default:
case 0: return new Wind();
case 1: return new Percussion();
case 2: return new Stringed();
case 3: return new Brass();
case 4: return new WoodWind();
}
}
public static void main(String[] args){
Music8 m = new Music8();
//Upcasting during addition to the array:
Instrument[] orchestra = new Instrument[7];
for(int i = 0;i < orchestra.length;i++)
orchestra[i] = m.tune();
for(Instrument in : orchestra)
in.play();//这句话用不了
}
}
------解决思路----------------------
in.play(Node n)不是要传个参数么...
------解决思路----------------------
in.play(),传个参数呀
------解决思路----------------------
错误提示已经很明显啦。
另外提点建议:
private Random rand = new Random(23);
//seed不变的话nextInt是固定值,这个是伪随机不是真正的随机
rand.nextInt(3);
//在前面那个是23改为空的情况下,只能产生0-2的数,3和4无法生成