/*
人的抽象类
*/
abstract class Person
{
//姓名
private String name;
//年龄
private int age;
//无参构造
Person(){}
//getter/setter
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return age;
}
//吃饭
public abstract void eat();
//睡觉
public void sleep()
{
System.out.println("睡觉");
}
}
//运动的抽象类
abstract class Sporter extends Person
{
public Sporter(){}
//练习的方法
public abstract void train();
}
//教练的抽象类
abstract class Coach extends Person
{
public Coach(){}
//指导的方法
public abstract void teach();
}
//说英语的接口
interface SpeakEnglish
{
public abstract void speak();
}
//下面定义具体的类
class BasketballSporter extends Sporter
{
//重写抽象方法
public void eat()
{
System.out.println("篮球运动员吃牛排");
}
public void train()
{
System.out.println("每天打篮球很辛苦");
}
}
class BasketballCoach extends Coach
{
public void eat()
{
System.out.println("篮球教练吃两份牛排");
}
public void teach()
{
System.out.println("教篮球挺有意思的");
}
}
class TableTennisSporter extends Sporter implements SpeakEnglish
{
//重写抽象方法
public void eat()
{
System.out.println("乒乓球运动员喝加多宝");
}
public void train()
{
System.out.println("每天打乒乓球不辛苦");
}
//实现抽象方法
public void speak()
{
System.out.println("我们说的是什么鸟语啊!");
}
}
class TableTennisCoach extends Coach implements SpeakEnglish
{
//重写抽象方法
public void eat()
{
System.out.println("乒乓球教练喝王老吉");
}
public void teach()
{
System.out.println("每天指导打乒乓球jsut so so");
}
//实现抽象方法
public void speak()
{
System.out.println("这还用学,我教你们吧,不过得加工资!");
}
}
class Test
{
public static void main(String[] args)
{
BasketballSporter bss = new BasketballSporter();
bss.eat();
bss.sleep();
bss.train();
BasketballCoach bsc = new BasketballCoach();
bsc.eat();
bsc.sleep();
bsc.teach();
TableTennisSporter tts = new TableTennisSporter();
tts.eat();
tts.sleep();
tts.train();
tts.speak();
TableTennisCoach ttc = new TableTennisCoach();
ttc.eat();
ttc.sleep();
ttc.teach();
ttc.speak();
}
} |
|