30.Java基础_接口_抽象类_具体类实现案例

30.Java基础_接口_抽象类_具体类实现案例

30.Java基础_接口_抽象类_具体类实现案例

 public interface SpeakEnglish {
public abstract void speak();
} 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 Coach extends Person{
public Coach() {
}
public Coach(String name, int age) {
super(name, age);
}
public abstract void teach();
} public abstract class Player extends Person{
public Player() {
}
public Player(String name, int age) {
super(name, age);
}
public abstract void study();
} public class BasketballCoach extends Coach{
public BasketballCoach() {
}
public BasketballCoach(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("篮球教练吃炒饭!");
}
@Override
public void teach() {
System.out.println("篮球教练教运球和投篮!");
}
} public class BasketballPlayer extends Player{
public BasketballPlayer() {
}
public BasketballPlayer(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("篮球运动员吃炒年糕");
}
@Override
public void study() {
System.out.println("篮球运动员学习运球和投篮!");
}
} public class PingPangCoach extends Coach implements SpeakEnglish{
public PingPangCoach() {
}
public PingPangCoach(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("乒乓球教练吃炒面");
}
@Override
public void teach() {
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 eat() {
System.out.println("乒乓球运动员吃炒粉丝");
}
@Override
public void study() {
System.out.println("乒乓球运动员学习发球和接球");
}
@Override
public void speak() {
System.out.println("乒乓球运动员说英语");
}
} //测试类
public class test {
public static void main(String[] args) {
//创建对象
PingPangPlayer ppp=new PingPangPlayer("王浩",30);
System.out.println(ppp.getName()+","+ppp.getAge());
ppp.eat();
ppp.study();
ppp.speak();
System.out.println("------------------");
BasketballPlayer bp=new BasketballPlayer("姚明",40);
System.out.println(bp.getName()+","+bp.getAge());
bp.eat();
bp.study();
}
}
上一篇:第一章:Javascript语言核心


下一篇:html/css/javascript练习代码