复制代码
- //:Music5.java
- //Interfaces
- importjava.util.*;
- interfaceInstrument5{
- //Compile-timeconstant:
- inti=5;//static&final
- //Cannothavemethoddefinitions:
- voidplay();//Automaticallypublic
- Stringwhat();
- voidadjust();
- }
- classWind5implementsInstrument5{
- publicvoidplay(){
- System.out.println("Wind5.play()");
- }
- publicStringwhat(){return"Wind5";}
- publicvoidadjust(){}
- }
- classPercussion5implementsInstrument5{
- publicvoidplay(){
- System.out.println("Percussion5.play()");
- }
- publicStringwhat(){return"Percussion5";}
- publicvoidadjust(){}
- }
- classStringed5implementsInstrument5{
- publicvoidplay(){
- System.out.println("Stringed5.play()");
- }
- publicStringwhat(){return"Stringed5";}
- publicvoidadjust(){}
- }
- classBrass5extendsWind5{
- publicvoidplay(){
- System.out.println("Brass5.play()");
- }
- publicvoidadjust(){
- System.out.println("Brass5.adjust()");
- }
- }
- classWoodwind5extendsWind5{
- publicvoidplay(){
- System.out.println("Woodwind5.play()");
- }
- publicStringwhat(){return"Woodwind5";}
- }
- publicclassMusic5{
- //Doesntcareabouttype,sonewtypes
- //addedtothesystemstillworkright:
- staticvoidtune(Instrument5i){
- //...
- i.play();
- }
- staticvoidtuneAll(Instrument5[]e){
- for(inti=0;i<e.length;i++)
- tune(e[i]);
- }
- publicstaticvoidmain(String[]args){
- Instrument5[]orchestra=newInstrument5[5];
- inti=0;
- //Upcastingduringadditiontothearray:
- orchestra[i++]=newWind5();
- orchestra[i++]=newPercussion5();
- orchestra[i++]=newStringed5();
- orchestra[i++]=newBrass5();
- orchestra[i++]=newWoodwind5();
- tuneAll(orchestra);
- }
- }///:~
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |