📄 chap4-18.txt
字号:
// 程序4-18
abstract class instrument{
abstract void play( ); // 抽象方法
}
// wind对父类中的抽象方法play进行了实现,因此它不再是抽象类
class wind extends instrument{
void play( ){ System.out.println("wind play!"); }
}
// percussion对父类中的抽象方法play进行了实现,因此它也不再是抽象类
class percussion extends instrument{
void play( ){ System.out.println("percussion play!"); }
}
// stringed对父类中的抽象方法play进行了实现,因此它也不再是抽象类
class stringed extends instrument{ // 对父类中的抽象方法play进行了实现
void play( ){ System.out.println("stringed play!"); }
}
class woodWind extends wind{ // 覆盖了父类wind中的play方法
void play( ){ System.out.println("woodWind play!"); }
}
class brass extends wind{ // 覆盖了父类wind中的play方法
void play( ){ System.out.println("brass play!"); }
}
public class music { // 主类
static void tuneAll( instrument e[ ]){
for(int i=0;i<e.length;i++)
e[i].play( );
}
public static void main(String [ ] args){
instrument orchestra[ ] = new instrument[5];
int i=0;
orchestra[i++]=new wind( );
orchestra[i++]=new percussion( );
orchestra[i++]=new stringed( );
orchestra[i++]=new woodWind( );
orchestra[i++]=new brass( );
tuneAll(orchestra);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -