Java 继承——1
实现如下类之间的继承关系,并编写Music类来测试这些类。
package text1; public class Instrument { //输出弹奏乐器 public void play() { System.out.println("乐器"); } }
package text1; public class Wind extends Instrument { //输出弹奏wind public void play() { System.out.println("弹奏wind"); } //输出调用wind的play2 public void play2() { Wind p=new Wind(); p.play(); } }
package text1; public class Brass extends Instrument { //输出弹奏brass public void play() { System.out.println("弹奏brass"); } //输出调用wind的play2 public void play2() { Brass b=new Brass(); b.play(); } }
package text1; public class Music { //调用对象i的play方法 public static void turn(Instrument i) { i.play(); } public static void main(String[] args) { // TODO 自动生成的方法存根 Wind w=new Wind();//调用wind方法 turn(w); Brass b =new Brass();//调用brass方法 turn(b); } }