package com.imooc; interface SpeakEnglish{ public abstract void speakEnglish(); } abstract class Person{ String name; int 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 Person(){} public Person(String name, int age){ this.name = name; this.age = age; } public abstract void eat(); public void sleep(){ System.out.println("睡觉吧"); } } abstract class Player extends Person{ public Player(){} public Player(String name, int age){ super(name, age); } public abstract void study(); } abstract class Coach extends Person{ public Coach(){} public Coach(String name, int age){ this.name = name; this.age = age; } public abstract void teach(); } class PingPangPlayer extends Player implements SpeakEnglish{ public PingPangPlayer(){} public PingPangPlayer(String name, int age){ super(name, age); } public void eat(){ System.out.println("乒乓球运动员吃饭"); } public void study(){ System.out.println("乒乓球运动员学习"); } public void speakEnglish(){ System.out.println("乒乓球运动员说英语"); } } class BasketPlayer extends Player{ public BasketPlayer(){} public BasketPlayer(String name, int age){ super(name, age); } public void eat(){ System.out.println("篮球运动员吃饭"); } public void study(){ System.out.println("篮球运动员学习"); } } class BasketCoach extends Coach{ public BasketCoach(){} public BasketCoach(String name, int age){ super(name, age); } public void eat(){ System.out.println("篮球教练吃饭"); } public void teach(){ System.out.println("篮球教练教学"); } } class PingPangCoach extends Coach implements SpeakEnglish{ public PingPangCoach(){} public PingPangCoach(String name, int age){ super(name, age); } public void eat(){ System.out.println("乒乓球教练吃饭"); } public void teach(){ System.out.println("乒乓球教练上课"); } public void speakEnglish(){ System.out.println("乒乓球教练说英语"); } } public class InterfaceDemo5 { public static void main(String[] args){ PingPangPlayer ppp = new PingPangPlayer(); ppp.setName("林青霞"); ppp.setAge(27); System.out.println(ppp.getName()+"---"+ppp.getAge()); ppp.eat(); ppp.sleep(); ppp.study(); PingPangCoach ppc = new PingPangCoach("明明", 21); System.out.println(ppc.getName()+"---"+ppc.getAge()); ppc.eat(); ppc.sleep(); ppp.study(); BasketPlayer bp = new BasketPlayer(); bp.setName("明明"); bp.setAge(21); System.out.println(bp.getName()+"---"+bp.getAge()); bp.eat(); bp.sleep(); bp.study(); BasketCoach bc = new BasketCoach("小明", 20); bc.eat(); bc.sleep(); } }
接口和继承
/* * 类与类: 继承关系,只能单继承,可以多层继承 类与接口: 实现关系,可以单实现,也可以多实现 接口与接口: 继承关系,可以单继承,也可以多继承 * */ interface Father{ public abstract void show(); } interface Mother{ public abstract void show2(); } interface Sister extends Father, Mother{ } class Son extends Object implements Father, Mother{ public void show(){ System.out.println("show son"); } public void show2(){ System.out.println("show son2"); } } public class InterfaceDemo2 { public static void main(String[] args){ } }
抽象类使用接口进行功能扩展
interface Jumpping{ public abstract void jump(); } abstract class Animal{ private String name; private int age; public Animal(){} public Animal(String name, int age){ this.name = name; this.age = age; } public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } public void sleep(){ System.out.println("睡觉啰"); } public abstract void eat(); } class Cat extends Animal{ public Cat(){} public Cat(String name, int age){ super(name, age); } public void eat(){ System.out.println("猫吃鱼"); } } class JumpCat extends Cat implements Jumpping{ public JumpCat(){} public JumpCat(String name, int age){ super(name, age); } public void jump(){ System.out.println("猫跳高"); } } public class InterfaceDemo3 { public static void main(String[] args){ JumpCat jp = new JumpCat(); jp.setName("Tom"); jp.setAge(3); System.out.println(jp.getName()+"------"+jp.getAge()); jp.eat(); jp.sleep(); jp.jump(); } }
运动员教练案例