Java-继承,多态练习09-22-01

1.实现如下类之间的继承关系,并编写Music类来测试这些类。

Java-继承,多态练习09-22-01

Java-继承,多态练习09-22-01

父类:

package com.lianxi;

public class Instrument {
//属性
private String yueqi; public String getYueqi() {
return yueqi;
} public void setYueqi(String yueqi) {
this.yueqi = yueqi;
}
//构造方法
public Instrument(String yueqi)
{
this.yueqi=yueqi;
} public Instrument() {
super();
} //成员方法
public void play()
{
System.out.println("弹奏乐器名称:"+yueqi);
}
}
package com.lianxi;

public class Brass extends Instrument {

    //方法重写
public void play()
{
System.out.println("弹奏Brass");
} public void play2()
{
System.out.println("调用Barss的play2");
} }
package com.lianxi;

public class Wind extends Instrument {

    //方法重写
public void play()
{
System.out.println("弹奏Wind");
} public void play2()
{
System.out.println("调用Wind的play2");
}
}

测试:

package com.lianxi;

public class Music {

    public static void main(String[] args) {
Instrument ins=new Instrument();
tune(ins);
Wind w=new Wind();
Brass b=new Brass();
w.play();
w.play2();
b.play();
b.play2(); }
public static void tune(Instrument i)
{
i.setYueqi("吉他");
i.play();
System.out.println(i.getYueqi());
}

结果:

Java-继承,多态练习09-22-01

上一篇:框架学习笔记之Maven简介和配置


下一篇:Oracle创建自增ID