package c07;class Note {
private int value;
private Note(int val) { value = val; } //构造器方法
public static final Note //创建三个Note对象
middleC = new Note(0),
cSharp = new Note(1),
cFlat = new Note(2);
} class Instrument {
public void play(Note n) //play方法调用Note类的一个对象
{
System.out.println("Instrument.play()");
}
}
class Wind extends Instrument // Wind类继承了Instrument类
{
public void play(Note n) {
System.out.println("Wind.play()");
}
}public class Musicc
{
public static void tune(Instrument i)
{
i.play(Note.middleC);
}
public static void main(String[] args)
{
Wind flute = new Wind();
tune(flute);
/**我有点不明白tune方法调用是Musicc类的静态方法tune之后,
*接着执行i.play()这个方法,
* 但结果怎么会是Wind.play()?
*它并没有调用Wind类的play()方法?它们两者之间有什么联系?
*请你们帮我仔细的讲讲,谢谢!
*/
}
}
private int value;
private Note(int val) { value = val; } //构造器方法
public static final Note //创建三个Note对象
middleC = new Note(0),
cSharp = new Note(1),
cFlat = new Note(2);
} class Instrument {
public void play(Note n) //play方法调用Note类的一个对象
{
System.out.println("Instrument.play()");
}
}
class Wind extends Instrument // Wind类继承了Instrument类
{
public void play(Note n) {
System.out.println("Wind.play()");
}
}public class Musicc
{
public static void tune(Instrument i)
{
i.play(Note.middleC);
}
public static void main(String[] args)
{
Wind flute = new Wind();
tune(flute);
/**我有点不明白tune方法调用是Musicc类的静态方法tune之后,
*接着执行i.play()这个方法,
* 但结果怎么会是Wind.play()?
*它并没有调用Wind类的play()方法?它们两者之间有什么联系?
*请你们帮我仔细的讲讲,谢谢!
*/
}
}
书上不是讲的很详细了~建议你再好好看看前面的多态部分
就是多态中向上转型的奥妙了