12001 Java继承类,抽象类,具体类实例,附源代码 – 恵玩科技

//定义一个说英语的接口

//interface class speakEnglish 错误的写法 接口类不需要加class
interface speakEnglish
{
	public abstract void speak();
}
//定义一个具体类使用接口---->>>接口实现类
class pingpongAthlete
{
	public void speak(){
		System.out.println("乒乓球运动员需要学习英语以便出国交流!");
	}
}
//定义一个人的抽象类
abstract class Person
{
	private String name;
	private int age;
	public Person(){}
	public Person(String name,int age){
		this.name=name;
		this.age=age;
	}
	//成员方法
	public String getName(){
		return name;
	}
	public void setName(String name){
		this.name=name;
	}
	public int getAge(){
		return age;
	}
	public void setAge(int age){
		this.age=age;
	}
	//成员方法
	
	public void sleep(){
		System.out.println("人类睡觉了");
	}
	//定义一个抽象方法,吃饭
	public abstract void eat();//抽象方法不能有大括号
}
//定义抽象运动员类
abstract class Player extends Person
{
	public Player(){}
	public Player(String name,int age){
		super(name,age);
	}
	//学习
	public abstract void study();
}
//定义教练类
abstract class Coach extends Person
{
	public Coach(){}
	public Coach(String name,int age){
		super(name,age);
	}
	//教
	public abstract void teach();
}
//定义乒乓球运动员具体类
class PingPangPlayer extends Player implements speakEnglish
{
	public PingPangPlayer(){}
	public PingPangPlayer(String name,int age){
		super(name,age);
	}
	//吃饭
	public void eat(){
		System.out.println("乒乓球运动员吃大白菜,喝小米粥");
	}
	//学习
	public void study(){
		System.out.println("乒乓球运动员学习如何发球接球");
	}
	//说英语
	public void speak(){
		System.out.println("乒乓球运动员练习说英语");
	}

}
//定义篮球运动员具体类
class basketballPlayer extends Player
{
	public basketballPlayer(){}
	public basketballPlayer(String name,int age){
		super(name,age);
	}
	//吃饭
	public void eat(){
		System.out.println("篮球运动员吃牛肉,喝牛奶");
	}
	//学习
	public void study(){
		System.out.println("篮球运动员学习如何成为灌篮高手");
	}
}
//定义乒乓球教练具体类
class PingPangCoach extends Coach implements speakEnglish
{
	public PingPangCoach(){}
	public PingPangCoach(String name,int age){
		super(name,age);
	}
	//吃饭
	public void eat(){
		System.out.println("乒乓球教练吃麻辣香锅");
	}
	//教
	public void teach(){
		System.out.println("乒乓球教练教乒乓球");
	}
	//说英语
	public void speak(){
		System.out.println("乒乓球教练说英语");
	}
}
//定义一个篮球教练具体类
class basketballCoach extends Coach
{
	public basketballCoach(){}
	public basketballCoach(String name,int age){
		super(name,age);
	}
	//吃饭
	public void eat(){
		System.out.println("篮球教练吃火锅");
	}
	//教
	public void teach(){
		System.out.println("篮球教练教灌篮");
	}
}


class InterfaceDemo1
{
	public static void main(String[] args){
		
		pingpongAthlete p=new pingpongAthlete();//通过测试 ping-pong这种写法是错误的
		p.speak();
		System.out.println("-----------------------------");
		basketballPlayer bp=new basketballPlayer();
		bp.eat();
		bp.study();
		


		//System.out.println("HelloWord");

	}
}

 

打赏作者