/*
* 篮球运动员和教练
乒乓球运动员和教练
现在篮球运动员和教练要出国访问,需要学习英语
请根据你所学的知识,分析出来哪些是类,哪些是抽象类,哪些是接口
*/
为何学英语是接口?
继承的特点是是单一继承,各个成员之间都有了对应关系。需要利用接口的多继承特点。
package day24_1接口_interface; public class InterfaceTest { public static void main(String[] args) { BasketballPlayer bbp = new BasketballPlayer(); bbp.name = "庆阳"; bbp.age = 18; bbp.gender = "男"; bbp.eat(); bbp.study(); bbp.speaking(); System.out.println("-----------"); PingpangCoach ppc = new PingpangCoach(); ppc.age = 20; ppc.eat(); ppc.teach(); } } //人 class Person { String name; int age; String gender; public void eat() { System.out.println("吃饭"); } public void sleep() { System.out.println("睡觉"); } } //说英语 interface speak { public abstract void speaking(); } //运动员 abstract class Player extends Person { // public abstract void study(); } //教练 abstract class Coach extends Person { public abstract void teach(); } //篮球运动员 class BasketballPlayer extends Player implements speak { @Override public void study() { System.out.println("学篮球"); } @Override public void speaking() { System.out.println("说英语"); } } //乒乓球运动员 class PingpangPlayer extends Player { @Override public void study() { System.out.println("学乒乓"); } } //篮球教练 class BasketballCoach extends Coach implements speak { @Override public void teach() { System.out.println("教 篮球"); } @Override public void speaking() { System.out.println("说英语"); } } //乒乓球教练 class PingpangCoach extends Coach { @Override public void teach() { System.out.println("教 乒乓"); } }
输出如下