设计宠物类,用户可以自由选择养猫还是养狗,可以给宠物起名字,还可以实现喂食互动的功能,宠物需要有饱食度和快乐度

这篇博客展示了如何使用Java创建一个动物类(Animal)作为超类,并且派生出猫类(Cat)。动物类包含id、name、food和happiness属性,以及饱食度和快乐度的方法。猫类继承动物类并增加了eat方法。在AnimalTest类中,用户可以选择领养猫,并通过输入食物量与宠物进行喂食和互动。程序实现了根据宠物的饱食度和快乐度给出不同反馈的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package Excesise;

public class Animal {
    protected int id;
    protected String name;
    protected int food;
    protected int happiness;
    public Animal(){}
    public Animal(int id,String name,int food){
        this.id = id;
        this.name = name;
        this.food = food;
        happiness = 100;
    }
    public void full(int food){//饱食度的方法
        if (food == 100) {
            System.out.println("吃饱了,睡觉觉,谢谢主人^_^");
        } else if(food > 100){
            System.out.println("吃不下那么多啦~");
        }

    }
    public void happy(int happiness){
        if(happiness == 100){
            System.out.println(name + "说:幸福感满满☺");
        } else if (happiness > 0 ){
            System.out.println(name + ":再来点☹");
        } else if (happiness <= 0) {
            System.out.println(name + "悲伤地坐在小角落心碎☹");
        }
    }
    public void unhappy(int food){
        if (food < 50) {
            System.out.println("我饿了");
        } else if (food > 0){
            System.out.println("快要饿死了");
        } else {
            System.out.println("你的宠物已经饿死了");
        }
    }
}

创建了一个动物的超类

package Excesise;

public class Cat extends Animal{
    public Cat(int id,String name,int food){
        this.id = id;
        this.name = name;
        this.food = food;
        happiness = 50;
    }
    public void eat(int food){
        this.food = food;
        happiness += food;
    }
}

又是一个小猫的派生类

package Excesise;
/*设计宠物类,用户可以自由选择养猫还是养狗,
可以给宠物起名字,还可以实现喂食互动的功能,宠物需要有饱食度和快乐度*/
import java.util.Scanner;

public class AnimalTest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要领养的宠物的序号:1、猫,2、狗");
        int id = scanner.nextInt();
        switch (id){
            case 1:
                cat(id);
                break;
            case 2:
                cat(id);
            default:
                System.out.println("这个小动物还没来哦~请再等等~~");
        }

    }


    public static void cat(int id){
        System.out.println("给宠物起个名字吧~");
        Scanner scanner1 = new Scanner(System.in);
        String name = scanner1.nextLine();
        System.out.println("恭喜你领养" + name + "成功啦~快买点食物喂喂它吧~");
        System.out.println("想买多少食物呢?~1-100哦~");
        int food = scanner1.nextInt();
        Cat cat = new Cat(id,name,food);
        int food1 = 0;
        int full = food + food1;
        if(full <= 0){
            cat.happy(full);
        }
        while(full > 0 && full <= 100){//50   60
            if (full == 100){
                cat.full(full);
                break;
            } else {
                cat.eat(full);//50   60
                cat.happy(cat.happiness);
                System.out.println("再来点食物吧~");
                food1 = scanner1.nextInt();//10 20
                full = full + food1;//60  80
                if (full == 100){
                    cat.full(full);
                    break;
                } else if(full < 100){
                    cat.eat(full);//60
                    cat.happy(cat.happiness);
                    System.out.println(name + "吃的很开心");
                    continue;
                }
            }
            if (full == 100){
                cat.happy(full);
                break;
            }
        }
    }
}

功能简单了点,用一个派生类就做出来了,可是还有个小狗类,感觉这个互动差不多啊,还是我没有弄明白题目的意思?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值