7-8 定义接口(Biology、Animal)、类(Person)、子类(Pupil)

(1)定义Biology(生物)、Animal(动物)2个接口,其中Biology声明了抽象方法breathe( ),Animal声明了抽象方法eat( )sleep( )
(2)定义一个类Person(人)实现上述2个接口,实现了所有的抽象方法,同时自己还有一个方法think( )。breathe()、eat()、sleep()、think()四个方法分别输出:
我喜欢呼吸新鲜空气
我会按时吃饭
早睡早起身体好
我喜欢思考
(3)定义Person类的子类Pupil(小学生),有私有的成员变量school(学校),公有的成员方法setSchool( )getSchool( )分别用于设置、获取学校信息。
(4)在测试类Main中,用Pupil类创建一个对象zhangsan。尝试从键盘输入学校信息给zhangsan,获取到该信息后输出该学校信息,格式为“我的学校是XXX”;依次调用zhangsan的breathe()、eat()、sleep()、think()方法。

输入格式:

从键盘输入一个学校名称(字符串格式)

输出格式:

第一行输出:我的学校是XXX(XXX为输入的学校名称)
第二行是breathe()方法的输出
第三行是eat()方法的输出
第四行是sleep()方法的输出
第五行是think()方法的输出

输入样例:

在这里给出一组输入。例如:

新余市逸夫小学

输出样例:

在这里给出相应的输出。例如:

我的学校是新余市逸夫小学
我喜欢呼吸新鲜空气
我会按时吃饭
早睡早起身体好
我喜欢思考

代码展示

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Pupil zhangsan=new Pupil();
        zhangsan.setSchool(sc.next());
        System.out.println("我的学校是"+zhangsan.getSchool());
        zhangsan.breathe();
        zhangsan.eat();
        zhangsan.sleep();
        zhangsan.think();
    }
}

class Pupil extends Person{
    private String school;

    public Pupil() {
    }

    public Pupil(String school) {
        this.school = school;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }
}

class Person implements Biology,Animal{

    @Override
    public void breathe() {
        System.out.println("我喜欢呼吸新鲜空气");
    }

    @Override
    public void eat() {
        System.out.println("我会按时吃饭");
    }

    @Override
    public void sleep() {
        System.out.println("早睡早起身体好");
    }

    public void think(){
        System.out.print("我喜欢思考");
    }
}

interface Biology{
    public abstract void breathe();
}

interface Animal{
    public abstract void eat();
    public abstract void sleep( );
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值