乒乓球运动员和篮球运动员,乒乓球教练和篮球教练。为了出国交流,跟乒乓球相关的人员都需要学习英语。
先创建一个包,在包中创建如下:
1.抽象类
Person抽象类
package jiekouchouxiang;public abstract class Person {private String name;private int age;public Person(){}public Person(String name,int age){this.name=name;this.age=age;}public abstract void eat();public void sleep(){System.out.println("睡觉要有足够的睡眠");}public void show(){System.out.println("姓名:"+name);System.out.println("年龄:"+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;}}
Player抽象类
package jiekouchouxiang;public abstract class Player extends Person {public Player(){super();}public Player(String name,int age){super();}public abstract void student();}
Coach抽象类
package jiekouchouxiang;public abstract class Coach extends Person{public Coach(){super();}public Coach(String name,int age){super();}public abstract void teacher();}
2.接口
English接口
package jiekouchouxiang;public interface English {void speak();}
3.具体类
PingpangPlayer
package jiekouchouxiang;public class PingpangPlayer extends Player implements English {@Overridepublic void student(){System.out.println("乒乓球运动员要学习发球和接球技巧");}@Overridepublic void eat(){System.out.println("乒乓球运动员要吃清淡的饭菜");}@Overridepublic void speak(){System.out.println("乒乓球运动员要学习英语");}}
PingpangCoach
package jiekouchouxiang;public class PingpangCoach extends Coach implements English {@Overridepublic void teacher(){System.out.println("乒乓球教练要教队员发球和接球技巧");}@Overridepublic void eat(){System.out.println("乒乓球教练要吃清淡的饭菜");}@Overridepublic void speak(){System.out.println("乒乓球教练要学习英语");}}
BasketballPlayer
package jiekouchouxiang;public class BasketballPlayer extends Player {@Overridepublic void student(){System.out.println("篮球运动员要学习运球和投篮技巧");}@Overridepublic void eat(){System.out.println("篮球运动员要吃高蛋白的饭菜");}}
BasketballCoach
package jiekouchouxiang;public class BasketballCoach extends Coach {@Overridepublic void teacher(){System.out.println("篮球教练要教队员运球和投篮技巧");}@Overridepublic void eat(){System.out.println("篮球教练要吃高蛋白的饭菜");}}
jiekouchouxiang
package jiekouchouxiang;public class jiekouchouxiang {public static void main(String[] args) {PingpangPlayer ppp=new PingpangPlayer();ppp.setName("马龙");ppp.setAge(18);ppp.show();ppp.eat();ppp.student();ppp.speak();System.out.println("*********************");PingpangCoach ppc=new PingpangCoach();ppc.setName("邓亚萍");ppc.setAge(38);ppc.show();ppc.eat();ppc.teacher();ppc.speak();System.out.println("*********************");BasketballPlayer bp=new BasketballPlayer();bp.setName("易建联");bp.setAge(24);bp.show();bp.eat();bp.student();System.out.println("*********************");BasketballCoach bc=new BasketballCoach();bc.setName("姚明");bc.setAge(35);bc.show();bc.eat();bc.teacher();System.out.println("*********************");}}