日期:2014-05-20  浏览次数:20728 次

急:有个关于JAVA向上转型的问题,请大家帮我看下;
我有个书上的程序不明白是哪里有问题,是关于向上转型的;
有四个类文件;
(1)
package   thinkinjava.c07;

class   Instrument   {
public   void   play(){}
static   void   tune(Instrument   i){
i.play();
}
}
(2)
package   thinkinjava.c07;

public   class   Wind   extends   Instrument   {
public   void   play(Note   n)   {
System.out.println( "Wind.play()   "   +   n);
}
}
(3)
package   thinkinjava.c07;

public   class   Note   {
private   String   noteName;

private   Note(String   noteName)   {
this.noteName   =   noteName;
}

public   String   toString()   {
return   noteName;
}

public   static   final   Note  
                              MIDDLE_C   =   new   Note( "Middle   C "),
            C_SHARP   =   new   Note( "C   Sharp "),  
                              B_FLAT   =   new   Note( "B   Flat ");
}
(4)
package   thinkinjava.c07;

public   class   Music   {
public   static   void   tune(Instrument   i){
i.play(Note.MIDDLE_C);       //这里提示i.play这行出错
}
public   static   void   main(String[]   agrs){
Wind   flute   =   new   Wind();
tune(flute);
}
}  

在这里我把tune()方法里的参数改成Instrument的子类Wind就是正常的,可是这种向上转型在书上说是允许的,我不明白这是哪里错了为什么错了,所以请大家帮帮我

------解决方案--------------------
向上转型是允许的
------解决方案--------------------
Instrument 没有 play(Note.MIDDLE_C) 这个方法 只有public void play(){}