- //运动员和教练
- package com.kxg;//包
- interface English//接口类
- {
- public abstract void speak();
- }
- 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 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)
- {
- super(name,age);
- }
- public abstract void teach();
- }
- //定义乒乓球运动员具体类
- class PingPangPlayer extends Player implements English
- {
- public PingPangPlayer(){}
- public PingPangPlayer(String name, int age)
- {
- super(name,age);
- }
- public void study()
- {
- System.out.println("学习乒乓球技术");
- }
- public void eat()
- {
- System.out.println("食堂吃饭");
- }
- public void speak()
- {
- System.out.println("Learn English");
- }
- }
- //定义篮球运动员具体类
- class BasketballPlayer extends Player implements English
- {
- public BasketballPlayer(){}
- public BasketballPlayer(String name, int age)
- {
- super(name,age);
- }
- public void study()
- {
- System.out.println("学习篮球技术");
- }
- public void eat()
- {
- System.out.println("食堂吃饭");
- }
- public void speak()
- {
- System.out.println("Learn English");
- }
- }
- //定义乒乓球教练具体类
- class PingPangCoach extends Coach implements English
- {
- public PingPangCoach(){}
- public PingPangCoach(String name, int age)
- {
- super(name,age);
- }
- public void teach()
- {
- System.out.println("教授乒乓球技术");
- }
- public void eat()
- {
- System.out.println("自己做饭");
- }
- public void speak()
- {
- System.out.println("Learn English");
- }
- }
- //定义篮球教练具体类
- class BasketballCoach extends Coach implements English
- {
- public BasketballCoach(){}
- public BasketballCoach(String name, int age)
- {
- super(name,age);
- }
- public void teach()
- {
- System.out.println("教授篮球技术");
- }
- public void eat()
- {
- System.out.println("自己做饭");
- }
- public void speak()
- {
- System.out.println("Learn English");
- }
- }
- public class InterfaceTest3
- {
- public static void main (String [] args)
- {
- //乒乓球运动员
- PingPangPlayer p = new PingPangPlayer("小明",18);
- System.out.println(p.getName()+":"+p.getAge());
- p.study();
- p.speak();
- p.eat();
- p.sleep();
- System.out.println("==================");
- //篮球运动员
- BasketballPlayer p2 = new BasketballPlayer("小黄",19);
- System.out.println(p2.getName()+":"+p2.getAge());
- p2.study();
- p2.speak();
- p2.eat();
- p2.sleep();
- System.out.println("==================");
- //乒乓球教练
- PingPangCoach c = new PingPangCoach("大周",40);
- System.out.println(c.getName()+":"+c.getAge());
- c.teach();
- c.speak();
- c.eat();
- c.sleep();
- System.out.println("==================");
- //篮球教练
- BasketballCoach c2 = new BasketballCoach("大飞",45);
- System.out.println(c2.getName()+":"+c2.getAge());
- c2.teach();
- c2.speak();
- c2.eat();
- c2.sleep();
- }
- }
复制代码 |