/*
关于抽象类和接口的综合练习:
乒乓球运动员和教练
篮球运动员和教练
为了出国交流,乒乓球运动员和教练需要说英语。
请你用所学知识,分析这里应该有哪些类,抽象类,接口.
*/
//定义讲英语接口类
interface SpeakEnglish
{
public abstract void speakEnglish();
}
//定义人抽象类
abstract class Person
{
//私有人共有属性
private String name;
private int age;
private String profession;
//定义无参构造
public Person(){}
//定义有参构造
public Person(String name,int age)
{
this.name = name ;
this.age = age;
}
//为无参构造提供公共访问
public void setName(String name)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
//定义人的功能
public void sleep()
{
System.out.println("人都要睡觉");
}
public abstract void eat();
public void show()
{
System.out.println("名字:"+name+",年龄:"+age);
}
}
//定义运动员类并继承人的类
class Athlete extends Person
{
public Athlete(){}
public void athleteTrain()
{
System.out.println("运动员训练");
}
public void eat()
{
System.out.println("运动员吃饭");
}
}
//定义教练类并继承人类
class Train extends Person
{
public Train(){}
public void athleteTrain()
{
System.out.println("教练教运动员");
}
public void eat()
{
System.out.println("教练吃饭吃饭");
}
}
//定义乒乓运动员继承运动员
class PingPangAthlete extends Athlete implements SpeakEnglish
{
public PingPangAthlete(){}
public void athleteTrain()
{
System.out.println("乒乓运动员学习打球,发球");
}
public void eat()
{
System.out.println("乒乓运动员吃五谷杂粮");
}
public void speakEnglish()
{
System.out.println("乒乓运动员为出国交流技术学习英语");
}
}
//定义篮球运动员继承运动员
class BasketballAthlete extends Athlete
{
public BasketballAthlete(){}
public void athleteTrain()
{
System.out.println("篮球运动员学习传球,扣篮");
}
public void eat()
{
System.out.println("篮球运动员吃肉牛奶");
}
}
//定义乒乓教练
class PingPangTrain extends Train implements SpeakEnglish
{
public PingPangTrain(){}
public void athleteTrain()
{
System.out.println("乒乓教练教运动员发球打球技术");
}
public void eat()
{
System.out.println("乒乓教练吃大米白菜");
}
public void speakEnglish()
{
System.out.println("乒乓教练为出国交流技术学习英语");
}
}
//定义篮球教练类
class BasketballTrain extends Train
{
public BasketballTrain(){}
public void athleteTrain()
{
System.out.println("篮球教练教运动员传球扣篮技术");
}
public void eat()
{
System.out.println("篮球教练吃馒头喝稀饭");
}
}
class PersonTest
{
public static void main(String[] args)
{
//创建乒乓员对象
PingPangAthlete ppa = new PingPangAthlete();
ppa.setName("邓亚萍");
ppa.setAge(23);
ppa.show();
ppa.athleteTrain();
ppa.eat();
ppa.speakEnglish();
System.out.println("-----------------------");
//创建乒乓教练对象
PingPangTrain ppt = new PingPangTrain();
ppt.setName("星星");
ppt.setAge(45);
ppt.show();
ppt.athleteTrain();
ppt.speakEnglish();
ppt.eat();
System.out.println("-----------------------");
//创建篮球运动员
BasketballAthlete bka = new BasketballAthlete();
bka.setName("乔丹");
bka.setAge(25);
bka.show();
bka.athleteTrain();
bka.eat();
System.out.println("-----------------------");
//创建篮球教练
BasketballTrain bbt = new BasketballTrain();
bbt.setName("迈克");
bbt.setAge(50);
bbt.show();
bbt.athleteTrain();
bbt.eat();
}
}
|
|