C-7 运动员和教练案例

题目:

    乒乓球运动员,篮球运动员。乒乓球教练,篮球教练。
    为了出国交流,跟乒乓球相关的人员都需要学习英语。

分析:

实现:

  1 /*
  2 教师版
  3 */
  4 interface SpeakEnglish{
  5     public abstract void speak();
  6 }
  7 abstract class Person{
  8     private String name;
  9     private int age;
 10     
 11     public Person(){}
 12     public Person(String name, int age){
 13         this.name = name;
 14         this.age = age;
 15     }
 16     public String getName(){
 17         return name;
 18     }
 19     public void setName(String name){
 20         this.name = name;
 21     }
 22     public int getAge(){
 23         return age;
 24     }
 25     public void setAge(int age){
 26         this.age = age;
 27     }
 28     //睡觉
 29     public void sleep(){
 30         System.out.println("睡觉");
 31     }
 32     //吃饭
 33     public abstract void eat();
 34 }
 35 
 36 abstract class Player extends Person{
 37     public Player(){}
 38     public Player(String name,int age){
 39         super(name,age);
 40     }
 41     
 42     //学习
 43     public abstract void study();
 44 }
 45 
 46 abstract class Coach extends Person{
 47     public Coach(){}
 48     public Coach(String name,int age){
 49         super(name,age);
 50     }
 51     
 52     //学习
 53     public abstract void teach();
 54 }
 55 //乒乓球运动员具体类
 56 class PingPangPlayer extends Player implements SpeakEnglish{
 57     public PingPangPlayer(){}
 58     public PingPangPlayer(String name, int age){
 59         super(name,age);
 60     }
 61     
 62     //吃饭
 63     public void eat(){
 64         System.out.println("乒乓球运动员吃菜饭");
 65     }
 66     //学习
 67     public void study(){
 68         System.out.println("乒乓球运动员学发球");
 69     }
 70     //学英语
 71     public void speak(){
 72         System.out.println("乒乓球运动员说英语");
 73     }
 74 }
 75 //篮球运动员具体类
 76 class BasketballPlayer extends Player{
 77     public BasketballPlayer(){}
 78     public BasketballPlayer(String name, int age){
 79         super(name,age);
 80     }
 81     //吃饭
 82     public void eat(){
 83         System.out.println("篮球运动员吃牛肉");
 84     }
 85     //学习
 86     public void study(){
 87         System.out.println("篮球运动员学投篮");
 88     }
 89     
 90 }
 91 //乒乓球教练具体类
 92 class PingPangCoach extends Coach implements SpeakEnglish{
 93     public PingPangCoach(){}
 94     public PingPangCoach(String name, int age){
 95         super(name,age);
 96     }
 97     
 98     //吃饭
 99     public void eat(){
100         System.out.println("乒乓球教练吃菜饭");
101     }
102     //学习
103     public void teach(){
104         System.out.println("乒乓球教练教发球");
105     }
106     //学英语
107     public void speak(){
108         System.out.println("乒乓球教练说英语");
109     }
110 }
111 
112 //篮球教练具体类
113 class BasketballCoach extends Coach{
114     public BasketballCoach(){}
115     public BasketballCoach(String name, int age){
116         super(name,age);
117     }
118     //吃饭
119     public void eat(){
120         System.out.println("篮球教练吃牛肉");
121     }
122     //学习
123     public void teach(){
124         System.out.println("篮球教练教投篮");
125     }
126     
127 }
128 
129 //测试
130 class SportDemo2{
131     public static void main(String[] args){
132     
133         //测试乒乓球运动员
134         PingPangPlayer ppp = new PingPangPlayer("王浩",33);
135         System.out.println("姓名:"+ppp.getName()+",年龄:"+ppp.getAge());
136         ppp.sleep();
137         ppp.eat();
138         ppp.study();
139         ppp.speak();
140         System.out.println("-----------");
141         
142         //测试篮球运动员
143         BasketballPlayer bp = new BasketballPlayer("姚明",36);
144         System.out.println("姓名:"+bp.getName()+",年龄:"+bp.getAge());
145         bp.sleep();
146         bp.eat();
147         bp.study();
148         System.out.println("-----------");
149         
150         //测试乒乓球教练
151         PingPangCoach ppc = new PingPangCoach("刘国梁",50);
152         System.out.println("姓名:"+ppc.getName()+",年龄:"+ppc.getAge());
153         ppc.sleep();
154         ppc.eat();
155         ppc.teach();
156         ppc.speak();
157         System.out.println("-----------");
158         
159         //测试篮球教练
160         BasketballCoach bc = new BasketballCoach("安西",52);
161         System.out.println("姓名:"+bc.getName()+",年龄:"+bc.getAge());
162         bc.sleep();
163         bc.eat();
164         bc.teach();
165         System.out.println("-----------");
166     }
167 }

运行:

姓名:王浩,年龄:33
睡觉
乒乓球运动员吃菜饭
乒乓球运动员学发球
乒乓球运动员说英语
-----------
姓名:姚明,年龄:36
睡觉
篮球运动员吃牛肉
篮球运动员学投篮
-----------
姓名:刘国梁,年龄:50
睡觉
乒乓球教练吃菜饭
乒乓球教练教发球
乒乓球教练说英语
-----------
姓名:安西,年龄:52
睡觉
篮球教练吃牛肉
篮球教练教投篮
-----------

 

posted @ 2015-09-06 15:20  小懒66  阅读(326)  评论(0编辑  收藏  举报