//接口
interface SpeakEnglish//定义讲英语的接口,因为讲英语是乒乓球的教练和运动员特有的,所以定义为接口
{
public abstract void SpeakingEnglish();
}
//人
abstract class Person
{
private String name;
private int age;
Person(){}
Person(String name,int age)
{
this.name=name;
this.age=age;
}
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("天晚了,要睡觉!!!");
}
public abstract void eat();//定义的抽象公共吃的方法,但不确定吃什么???
public void show()
{
System.out.println(name+"-------"+age);
}
}
//球员
abstract class Player extends Person
{
public abstract void exercise();//定义的抽象练习的方法,因不确定练习什么,所以定义为抽象的
}
//教练
abstract class Coach extends Person
{
public Coach(){}
public Coach(String name,int age)
{
super(name,age);
}
public abstract void introduce();//定义的抽象方法,因不确定教练教乒乓球还是篮球,所以定义为抽象的
}
//乒乓球球员
class PingPongPlayer extends Player implements SpeakEnglish
{
public void play()
{
System.out.println(getName()+"打乒乓球");
}
public void exercise()
{
System.out.println("我练习打乒乓球");
}
public void eat()
{
System.out.println("乒乓球运动员吃牛排");
}
public void SpeakingEnglish()
{
System.out.println("乒乓球运动员要讲英语");
}
}
//篮球球员
class BasketballPlayer extends Player
{
public void Play()
{
System.out.println("我打篮球");
}
public void exercise()
{
System.out.println("我练习打篮球");
}
public void eat()
{
System.out.println("篮球运动员吃猪排");
}
}
//乒乓球教练
class PingPongCoach extends Coach implements SpeakEnglish
{
public void SpeakingEnglish()
{
System.out.println("乒乓球教练要讲英语");
}
public void introduce()
{
System.out.println("我教乒乓球");
}
public void eat()
{
System.out.println("乒乓球教练吃牛肉");
}
}
//篮球教练
class BasketballCoach extends Coach
{
public BasketballCoach(String name,int age)
{
super(name,age);
}
public void introduce()
{
System.out.println("我教篮球");
}
public void eat()
{
System.out.println("篮球教练吃牛排");
}
}
class PlayerTest
{
public static void main(String[]args)
{
PingPongPlayer pp=new PingPongPlayer();
//第一种方法,通过setName(),setAge(),getName(),getAge()方法调用来实现
pp.setName("姐姐");
pp.setAge(13);
pp.play();
System.out.println(pp.getName()+"-------"+pp.getAge());
//第二种,通建立对象初始化赋值,必须有相对应的构造函数
BasketballCoach bb=new BasketballCoach("周总理",34);
bb.show();
}
} |