黑马程序员—Java基础(接口,类,抽象类综合小练习)

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

题目:教练和运动员案例:分为篮球与乒乓球两种,其中乒乓球运动员与教练需要学习英语,篮球运动员则不需要

具体分析:

运动员和教练案例分析

实现代码如下:

   1: package day06;
   2:  
   3:  
   4: public class Ex1 {
   5:     public static void main(String[] args){
   6:         //测试运动员(测试方法一样)
   7:         //乒乓球运动员
   8:         PingPangPlayer ppp = new PingPangPlayer();
   9:         ppp.setName("张三");
  10:         ppp.setAge(22);
  11:         System.out.println(ppp.getName()+"---"+ppp.getAge());
  12:         ppp.eat();
  13:         ppp.sleep();
  14:         ppp.study();
  15:         ppp.speak();
  16:         System.out.println("----------------");
  17:         //通过带参构造给数据
  18:         PingPangPlayer ppp1 = new PingPangPlayer("李四",23);    
  19:         System.out.println(ppp1.getName()+"---"+ppp.getAge());
  20:         ppp1.eat();
  21:         ppp1.sleep();
  22:         ppp1.study();
  23:         ppp1.speak();
  24:         System.out.println("----------------");
  25:     }
  26: }
  27: //定义说英语接口
  28: interface SpeakEnglish{
  29:     public abstract void speak();
  30: }
  31: //定义抽象人类
  32: abstract class Person{
  33:     private String name;
  34:     private int age;
  35:     public Person() {
  36:     }
  37:     public Person(String name,int age){
  38:         this.name = name;
  39:         this.age = age;
  40:     }
  41:     public String getName() {
  42:         return name;
  43:     }
  44:     public void setName(String name) {
  45:         this.name = name;
  46:     }
  47:     public int getAge() {
  48:         return age;
  49:     }
  50:     public void setAge(int age) {
  51:         this.age = age;
  52:     }
  53:     public void sleep(){
  54:         System.out.println("睡觉");
  55:     }
  56:     public abstract void eat();
  57: }
  58: //定义运动员抽象类
  59: abstract class Player extends Person{
  60:     public Player(){
  61:     }
  62:     public Player(String name, int age){
  63:         super(name,age);
  64:     }
  65:     public abstract void study();
  66: }
  67:  
  68: //定义教练抽象类
  69: abstract class Coach extends Person{
  70:     public Coach(){
  71:     }
  72:     public Coach(String name, int age){
  73:         super(name,age);
  74:     }
  75:     public abstract void teach();
  76: }
  77:  
  78: //定义乒乓球运动员具体类
  79: class PingPangPlayer extends Player implements SpeakEnglish{
  80:     public PingPangPlayer(){
  81:     }
  82:     public PingPangPlayer(String name, int age){
  83:         super(name, age);
  84:     }
  85:     @Override
  86:     public void speak() {
  87:         // TODO Auto-generated method stub
  88:         System.out.println("乒乓球运动员说英语");
  89:     }
  90:  
  91:     @Override
  92:     public void study() {
  93:         // TODO Auto-generated method stub
  94:         System.out.println("乒乓球运动员学习如何发球和接球");
  95:     }
  96:  
  97:     @Override
  98:     public void eat() {
  99:         // TODO Auto-generated method stub
 100:         System.out.println("乒乓球运动员吃大白菜,喝小米粥");
 101:     }
 102: }
 103: //定义篮球运动员具体类
 104: class BasketballPlayer extends Player{
 105:     public BasketballPlayer(){
 106:         
 107:     }
 108:     public BasketballPlayer(String name, int age){
 109:         super(name, age);
 110:     }
 111:     @Override
 112:     public void study() {
 113:         // TODO Auto-generated method stub
 114:         System.out.println("篮球运动员学习如何运球和投篮");
 115:     }
 116:  
 117:     @Override
 118:     public void eat() {
 119:         // TODO Auto-generated method stub
 120:         System.out.println("篮球运动员吃牛肉,喝牛奶");
 121:     }
 122: }
 123: //定义乒乓球教练具体类
 124: class PingPangCoach extends Coach implements SpeakEnglish{
 125:     public PingPangCoach(){    
 126:     }
 127:     public PingPangCoach(String name, int age){
 128:         super(name, age);
 129:     }
 130:     @Override
 131:     public void speak() {
 132:         // TODO Auto-generated method stub
 133:         System.out.println("乒乓球教练说英语");
 134:     }
 135:  
 136:     @Override
 137:     public void teach() {
 138:         // TODO Auto-generated method stub
 139:         System.out.println("乒乓球教练教如何发球和接球");
 140:     }
 141:  
 142:     @Override
 143:     public void eat() {
 144:         // TODO Auto-generated method stub
 145:         System.out.println("乒乓球教练吃小白菜,喝大米粥");
 146:     }
 147:     
 148: }
 149:  
 150: //定义篮球教练具体类
 151: class BasketballCoach extends Coach{
 152:  
 153:     @Override
 154:     public void teach() {
 155:         // TODO Auto-generated method stub
 156:         System.out.println("篮球教练教如何运球和投篮");
 157:     }
 158:  
 159:     @Override
 160:     public void eat() {
 161:         // TODO Auto-generated method stub
 162:         System.out.println("篮球教练吃羊肉,喝羊奶");
 163:     }
 164:     
 165: }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值