java面向对象编程实践

一、 题目

在这里插入图片描述
分析要点:

二、做题流程

  • 有哪些接口
  • 有哪些抽象类
  • 有哪些具体类(实现抽象方法在这里面)
  • 各种继承关系和实现关系

1、最顶层的抽象类和接口

Person.java

package coach_athlete;

/**
 * @className:Person;
 * @time:2022-04-28-23:20;
 * @author:Lee Ye;
 * @description:这是一个java示例;
 */
public abstract class Person {
    protected  String Name;
    protected int age;

    public Person(String Name, int age){
        this.Name = Name;
        this.age = age;
    }

    public void sleep(){
        System.out.println("人都是要睡觉的");
    }

    public abstract void eat();
}

speakEnglish.java

package coach_athlete;

public interface speakEnglish {

    void speak();
}

2、往下一层的抽象类

Athlete.java

package coach_athlete;

/**
 * @className:Athlete;
 * @time:2022-04-28-23:20;
 * @author:Lee Ye;
 * @description:这是一个java示例;
 */
public abstract class Athlete extends Person{

    protected String Name;
    protected  int age;
    public Athlete(String Name, int age){
        super(Name, age);
    }
    @Override
    public void eat(){
        System.out.println("乒乓球运动员吃大白菜,喝小米粥");
    }
    abstract void study();

}

Coach.java

package coach_athlete;

/**
 * @className:Coach;
 * @time:2022-04-28-23:20;
 * @author:Lee Ye;
 * @description:这是一个java示例;
 */
public abstract class Coach  extends  Person{
    protected String Name;
    protected int age;
    Coach(String Name, int age){
        super(Name, age);
    }

    abstract  void teach();
}

3、然后就是具体类的实现

PingPangPlayer.java

package coach_athlete;

/**
 * @className:PingPangPlayer;
 * @time:2022-04-28-23:32;
 * @author:Lee Ye;
 * @description:这是一个java示例;
 */
public class PingPangPlayer extends Athlete implements speakEnglish{
    protected String Name;
    protected int age;
    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("乒乓球运动员说英语");
    }
}

PingPongCoach.java

package coach_athlete;

/**
 * @className:PingPongCoach;
 * @time:2022-04-28-23:33;
 * @author:Lee Ye;
 * @description:这是一个java示例;
 */
public  class PingPongCoach extends Coach implements speakEnglish{
    protected String Name;
    protected int age;
    public PingPongCoach(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("乒乓球教练说英语");
    }
}

BasketBallPlayer.java

package coach_athlete;

/**
 * @className:BasketBallPlayer;
 * @time:2022-04-28-23:33;
 * @author:Lee Ye;
 * @description:这是一个java示例;
 */
public class BasketBallPlayer extends Athlete{

    protected String Name;
    protected int age;
    public BasketBallPlayer(String Name, int age){
        super(Name, age);
    }

    @Override
    public void eat(){
        System.out.println("篮球运动员吃牛肉,喝牛奶 ");
    }

    @Override
    public void study(){
        System.out.println("篮球运动员学习如何运球和投篮");
    }
}

BasketBallCoach.java

package coach_athlete;

/**
 * @className:BasketBallCoach;
 * @time:2022-04-28-23:33;
 * @author:Lee Ye;
 * @description:这是一个java示例;
 */
public class BasketBallCoach extends Coach{
    protected String Name;
    protected int age;
    public BasketBallCoach(String Name, int age){
        super(Name, age);
    }
    @Override
    public void eat(){
        System.out.println("篮球教练吃羊肉,喝羊奶");
    }
    @Override
    public void teach(){
        System.out.println("篮球教练教如何运球和投篮");
    }
}

4、最后写一个测试类

Test.java

package coach_athlete;
import java.util.Arrays;
import java.util.Scanner;
/**
 * @className:Test;
 * @time:2022-04-28-23:45;
 * @author:Lee Ye;
 * @description:这是一个java示例;
 */

/*
测试输入: 张继科 30 易建联 31 刘国梁 42 杜锋 37
预期输出: 张继科---30 人都是要睡觉的 乒乓球运动员吃大白菜,喝小米粥 乒乓球运动员学习如何发球和接球 乒乓球运动员说英语 ----------------
        易建联---31 人都是要睡觉的 篮球运动员吃牛肉,喝牛奶 篮球运动员学习如何运球和投篮 ----------------
        刘国梁---42 人都是要睡觉的 乒乓球教练吃小白菜,喝大米粥 乒乓球教练教如何发球和接球 乒乓球教练说英语 ----------------
        杜锋---37 人都是要睡觉的 篮球教练吃羊肉,喝羊奶 篮球教练教如何运球和投篮 ----------------
 */
public class Test {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String p1_name = input.next();
        int p1_age = input.nextInt();

        String p2_name = input.next();
        int p2_age = input.nextInt();

        String p3_name = input.next();
        int p3_age  = input.nextInt();

        String p4_name = input.next();
        int p4_age = input.nextInt();

        System.out.println(p1_name + "---"+p1_age);
        PingPangPlayer ppp = new PingPangPlayer(p1_name, p1_age);
        ppp.eat();
        ppp.study();
        ppp.speak();
        System.out.println("----------------");

        System.out.println(p2_name +"---"+p2_age);
        BasketBallPlayer bbp = new BasketBallPlayer(p2_name, p2_age);
        bbp.eat();
        bbp.study();
        System.out.println("----------------");

        System.out.println(p3_name + "---" + p3_age);
        PingPongCoach ppc = new PingPongCoach(p3_name, p3_age);
        ppc.eat();
        ppc.teach();
        ppc.speak();
        System.out.println("----------------");

        System.out.println(p4_name + "---" + p4_age);
        BasketBallCoach bbc = new BasketBallCoach(p4_name, p4_age);
        bbc.eat();
        bbc.teach();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值