【详细+超基础】Java-学习笔记 10

四、综合案例

4.1案例需求(理解)

我们现在有乒乓球运动员和篮球运动员,乒乓球教练和篮球教练。 为了出国交流,跟乒乓球相关的人员都需要学习英语。 请用所学知识分析,这个案例中有哪些具体类,哪些抽象类,哪些接口,并用代码实现。

4.2代码实现(应用)

抽象人类

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();
}

抽象运动员类

public abstract class Player extends Person {
     
public Player() {
     
}
public Player(String name, int age) {
     
super(name, age);
}
public abstract void study();
}

抽象教练类

public abstract class Coach extends Person {
     
public Coach() {
     
}
public Coach(String name, int age) {
     
super(name, age);
}
public abstract void teach();
}

学英语接口

public interface SpeakEnglish {
     
public abstract void speak();
}

蓝球教练

public class BasketballCoach extends Coach {
     
public BasketballCoach() {
     
}
public BasketballCoach(String name, int age) {
     
super(name, age);
}
@Override
public void teach() {
     
System.out.println("篮球教练教如何运球和投篮");
}
@Override
public void eat() {
     
System.out.println("篮球教练吃羊肉,喝羊奶");
}
}

乒乓球教练

public class PingPangCoach extends Coach implements SpeakEnglish {
     
public PingPangCoach() {
     
}
public PingPangCoach(String name, int age) {
     
super(name, age);
}
@Override
public void teach() {
     
System.out.println("乒乓球教练教如何发球和接球");
}
@Override
public void eat() {
     
System.out.println("乒乓球教练吃小白菜,喝大米粥");
}
@Override
public void speak() {
     
System.out.println("乒乓球教练说英语");
}
}

乒乓球运动员

public class PingPangPlayer extends Player implements SpeakEnglish {
     
public PingPangPlayer() {
     
}
public PingPangPlayer(String name, int age) {
     
super(name, age);
}
@Override
public void study() {
     
System.out.println("乒乓球运动员学习如何发球和接球");
}
@Override
public void eat() {
     
System.out.println("乒乓球运动员吃大白菜,喝小米粥");
}
@Override
public void speak() {
     
System.out.println("乒乓球运动员说英语");
}
}

篮球运动员

public class BasketballPlayer extends Player {
     
public BasketballPlayer() {
     
}
public BasketballPlayer(String name, int age) {
     
super(name, age);
}
@Override
public void study() {
     
System.out.println("篮球运动员学习如何运球和投篮");
}
@Override
public void eat() {
     
System.out.println("篮球运动员吃牛肉,喝牛奶");
}
}

你可能感兴趣的:(java,java,多态,抽象类,继承,接口)