//讲英语这行为定义为抽象接口
interface Speak
{
public abstract void English();
}
class person//定义一个父类
{
private String name;//姓名
private int age;//年龄
public person(){}//定义空参
public person(String name,int age)//定义实参
{
this.name = name;
this.age = age;
}
/*定义代码块*/
{
eat();
sleep();
System.out.println("在吃饭");
System.out.println("在睡觉");
}
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 void sleep()//成员函数--行为
{
System.out.println(name+"在睡觉");
}
public void eat()//成员函数--行为
{
System.out.println(name+"在吃饭");
}
public void show()//输出类函数
{
System.out.println("姓名:"+name+",年龄:"+age);
}
}
//定义一个乒乓球运动员的子类,继承person父类,实现English接口
class pingpongsport extends person implements Speak
{
public pingpongsport(){}
public pingpongsport(String name,int age)
{
super(name,age);
}
public void exercise()
{
System.out.println("乒乓球运动员在练习");
}
public void life()
{
eat();
sleep();
System.out.println("乒乓球运动员在吃饭");
System.out.println("乒乓球运动员在睡觉");
}
public void English()
{
System.out.println("乒乓球运动员讲英语");
}
}
//定义一个篮球运动员的子类,继承person父类
class basketballsport extends person
{
public basketballsport(){}
public basketballsport(String name,int age)
{
super(name,age);
}
public void exercise()
{
System.out.println("篮球运动员在练习");
}
public void life()
{
eat();
sleep();
System.out.println("篮球运动员在吃饭");
System.out.println("篮球运动员在睡觉");
}
}
//定义一个乒乓球教练的子类,继承person父类,实现English接口
class pingpongmaster extends person implements Speak
{
public pingpongmaster(){}
public pingpongmaster(String name,int age)
{
super(name,age);
}
public void coach()//指导
{
System.out.println("乒乓球教练在指导");
}
public void life()
{
eat();
sleep();
System.out.println("乒乓球教练在吃饭");
System.out.println("乒乓球教练在睡觉");
}
public void English()
{
System.out.println("乒乓球教练讲英语");
}
}
//定义一个篮球教练的子类,继承person父类
class basketballmaster extends person
{
public basketballmaster(){}
public basketballmaster(String name,int age)
{
super(name,age);
}
public void coach()//指导
{
System.out.println("篮球教练在指导");
}
public void life()
{
eat();
sleep();
System.out.println("篮球教练在吃饭");
System.out.println("篮球教练在睡觉");
}
}
class persontext
{
public static void main(String[] args)
{
Speak e = new pingpongsport();
e.English();
Speak e1 = new pingpongmaster();
e1.English();
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++");
pingpongsport p = new pingpongsport();
p.setName("邓亚萍");
p.setAge(25);
p.life();
System.out.println("====================================================");
pingpongmaster p1 = new pingpongmaster();
p1.setName("刘国梁");
p1.setAge(45);
p1.life();
System.out.println("----------------------------------------------------");
basketballsport b = new basketballsport();
b.setName("姚明");
b.setAge(35);
b.life();
System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\////");
basketballmaster b1 = new basketballmaster();
b1.setName("曼达斯");
b1.setAge(55);
b1.life();
}
} |