你的位置:首页 > Java教程

[Java教程]继承—Music


1 public class Instrument {2   3   public void play(){4     System.out.println("弹奏乐器");5   }

 1 public class Wind extends Instrument { 2  3   public void play(){ 4     System.out.println("弹奏Wind"); 5   } 6    7   public void play2(){ 8     System.out.println("调用Wind的play2"); 9   }10 11 }

 1 public class Brass extends Instrument { 2    3   public void play(){ 4     System.out.println("弹奏Brass"); 5   } 6   public void play2(){ 7     System.out.println("调用Brass的play2"); 8   } 9 10 }

 1 public class Music { 2   public static void tune (Instrument i){ 3     i.play(); 4   } 5    6  7   public static void main(String[] args) { 8      9     Wind w=new Wind();10     tune(w);11     Brass b=new Brass();12     tune(b);13     b.play2();14     15 16   }17 18 }

结果: