JavaSE-11.4【综合案例-多态&抽象类&接口】
package day2.yubaby04; /* 4 综合案例-多态&抽象类&接口 */ public class PersonDemo { public static void main(String[] args) { PingpangPlayer ppp = new PingpangPlayer(); ppp.setName("java"); ppp.setAge(23); System.out.println(ppp.getName() + "," + ppp.getAge()); ppp.eat(); ppp.study(); ppp.speak(); PingpangCoach ppc = new PingpangCoach("avaj", 44); System.out.println(ppc.getName() + "," + ppc.getAge()); ppc.eat(); ppc.teach(); ppc.speak(); BaketballPlayer bbp = new BaketballPlayer("C++", 30); System.out.println(bbp.getName() + "," + bbp.getAge()); bbp.eat(); bbp.study(); BaketballCoach bbc = new BaketballCoach(); bbc.setName("++C"); bbc.setAge(50); System.out.println(bbc.getName() + "," + bbc.getAge()); bbc.eat(); bbc.teach(); } }
package day2.yubaby04; public interface SpeakEnglish { // public abstract void speak(); void speak(); }
package day2.yubaby04; //抽象人类 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 void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public int getAge() { return age; } public abstract void eat(); }
package day2.yubaby04; //抽象运动员类 public abstract class Player extends Person{ public Player() { } public Player(String name, int age) { super(name, age); } public abstract void study(); }
package day2.yubaby04; //抽象教练类 public abstract class Coach extends Person{ public Coach() { } public Coach(String name, int age) { super(name, age); } public abstract void teach(); }
package day2.yubaby04; public class BaketballCoach extends Coach{ public BaketballCoach() { } public BaketballCoach(String name, int age) { super(name, age); } @Override public void teach() { System.out.println("篮球教练教打篮球"); } @Override public void eat() { System.out.println("篮球教练吃篮球吗"); } }
package day2.yubaby04; 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("乒乓球教练说英语"); } }
package day2.yubaby04; public class BaketballPlayer extends Player{ public BaketballPlayer() { } public BaketballPlayer(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("篮球运动员吃乒乓球吗"); } @Override public void study() { System.out.println("篮球运动员学习打篮球"); } }
package day2.yubaby04; 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("乒乓球运动员说英语"); } }