需求:我们现在有乒乓球运动员和篮球运动员,乒乓球教练和篮球教练。为了国际交流,跟乒乓球相关的人员都需要学习英语
。请用所有的知识分析,这案例中有哪些具体类,哪些抽象类,哪些接口,并用代码实现
思路:
1.定义英语接口 成员方法:说英语()
2.定义抽象类 成员变量:姓名、年龄;构造方法:无参、带参;成员方法:get/set方法,吃饭();
3.定义抽象教练类,继承人类 构造方法:无参、带参;成员方法:教();
4.定义运动员类,继承人类 构造方法:无参、带参;成员方法:学习()
5.定义抽象运动员类,继承人类 构造方法:无参、带参;成员方法:重写吃饭(){....};重写教(){....}
6.定义具体篮球教练类:继承教练类,实现说英语接口
构造方法:无参,带参;成员方法:重写吃饭(){.....},重写教(){....},重写说英语(){....}
7.定义具体篮球运动员类,继承运动员类 构造方法:无参、带参;成员方法:重写吃饭(){....},重写学习(){....}
8.定义具体乒乓球运动员类,继承运动员类,实现说英语接口
构造方法:无参,带参;成员方法:重写吃饭(){....},重写学习(){....},重写说英语(){....}
9.定义测试类,写代码测试
package com.itheme_44;
//教练类
public class BasketballCoach extends Coach {
public BasketballCoach() {
}
public BasketballCoach(String name, int age) {
super(name, age);
}
public void teach() {
System.out.println("篮球教练教如何运球和投篮");
}
public void eat() {
System.out.println("篮球教练吃羊肉,喝羊奶");
}
}
package com.itheme_44;
public class BasketballPlayer extends Play{
public BasketballPlayer() {
}
public BasketballPlayer(String name, int age) {
super(name, age);
}
public void study() {
System.out.println("篮球运动员学习如何运球和投篮");
}
public void eat() {
System.out.println("篮球运动员吃牛肉,喝牛奶");
}
}
package com.itheme_44;
//抽象教练类
public abstract class Coach extends Person {
public Coach(){
}
public Coach(String name,int age){
super(name,age);
}
public abstract void teach();
}
package com.itheme_44;
//抽象人类
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 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 abstract void eat();
}
package com.itheme_44;
/*
测试类
*/
public class PersonDemo {
public static void main(String[] args) {
//创建对象
// PingPangPlayer ppp = new PingPangPlayer();
// ppp.setName("王浩");
// ppp.setAge(30);
// System.out.println(ppp.getName() +"," +ppp.getAge());
// ppp.eat();
// ppp.study();
// ppp.speak();
// System.out.println("------");
PingPangPlayer pppp = new PingPangPlayer("王昱翔",30);
System.out.println(pppp.getName() + "," + pppp.getAge());
pppp.eat();
pppp.speak();
pppp.study();
BasketballPlayer bp = new BasketballPlayer("姚明",35);
// bp.setName("姚明");
// bp.setAge(35);
System.out.println(bp.getName() + "," + bp.getAge());
bp.eat();
bp.study();
}
}
package com.itheme_44;
public class PingPangCoach extends Coach implements SpeakEnglish {
public PingPangCoach() {
}
public PingPangCoach(String name, int age) {
super(name, age);
}
public void teach() {
System.out.println("乒乓球教练教如何发球和接球");
}
public void eat() {
System.out.println("乒乓球教练吃小白菜,喝大米粥");
}
public void speak() {
System.out.println("乒乓球教练说英语");
}
}
package com.itheme_44;
public class PingPangPlayer extends Play implements SpeakEnglish{
public PingPangPlayer() {
}
public PingPangPlayer(String name, int age) {
super(name, age);
}
public void study() {
System.out.println("乒乓球运动员学习如何发球和接球");
}
public void eat() {
System.out.println("乒乓球运动员吃大白菜,喝小米粥");
}
public void speak() {
System.out.println("乒乓球运动员说英语");
}
}
package com.itheme_44;
//抽象运动员类
public abstract class Play extends Person{
public Play(){
}
public Play(String name,int age){
super(name,age);
}
public abstract void study();
}
package com.itheme_44;
//说英语接口
public interface SpeakEnglish {
public abstract void speak();
}