9,重载,继承抽象类的喂猫狗的案例

1
public static void main(String[] args) {
    Dog dog = new Dog();
    Cat cat = new Cat();
    Person person1 = new Person();
    Person person2 = new Person();

    Scanner sc = new Scanner(System.in);
    OUT:while (true) {
        System.out.println("请作出选择:1,照顾猫咪,2,照顾狗狗,3,退出");
        int choose = sc.nextInt();
        switch (choose) {
            case 1://照顾猫咪
                person1.action(cat);
                break;
            case 2://照顾狗狗
                person1.action(dog);
                break;
            case 3:
                break OUT;
            default:
                System.out.println("输入不合法,请重新输入");
                break;
        }
    }
}

2

public void action(Pet pet){
    pet.action();
}

3

public Pet() {
}

public abstract void action();

4

public class Cat extends Pet{
    private int power = 100;
    private int intimacy = 0;

    public int getPower() {
        return power;
    }

    public void setPower(int power) {
        this.power = power;
    }

    public int getIntimacy() {
        return intimacy;
    }

    public void setIntimacy(int intimacy) {
        this.intimacy = intimacy;
    }

    public void show(){
        System.out.println("猫的体力值还剩:"+getPower()+"猫咪与主人的亲密度:"+getIntimacy());
    }
    public void action(){
        Scanner sc = new Scanner(System.in);
        OUT:while(true){
            show();
            System.out.println("请选择:1,给猫咪喂食  2,与猫咪互动增加亲密度,3,不玩了");
            int choose = sc.nextInt();
            switch (choose){
                case 1://给猫咪喂食
                    feed();
                    break;
                case 2://增加亲密度
                    addIntimacy();
                    break;
                case 3://离开
                    break OUT;
                default:
                    System.out.println("输入不合法,重新选择");
                    break ;
            }


        }

    }
//给猫咪喂食
    public void feed(){
        System.out.println("给猫咪喂食");
        setPower(power+=5);
        if (getPower()>=100){
            setPower(100);
            System.out.println("猫咪已经吃饱了,不能在吃了,可以尝试和猫咪互动");
            show();
        }else {
            System.out.println("猫咪体力值增加5");
            show();
        }
    }
//增加亲密度
    public void addIntimacy(){
        if (getPower() >=10){
            setPower(power-10);
            setIntimacy(intimacy+2);
            if (getIntimacy() >= 100){
                setIntimacy(100);
                System.out.println("亲密度已经达到上限");
            }
            show();
        }else {
            System.out.println("猫咪饿了,请喂食!");
        }
    }

}

5

public class Dog extends Pet{
    private int power = 100;
    private int intimacy = 0;


    public int getPower() {
        return power;
    }

    public void setPower(int power) {
        this.power = power;
    }

    public int getIntimacy() {
        return intimacy;
    }

    public void setIntimacy(int intimacy) {
        this.intimacy = intimacy;
    }
    public void show(){
        System.out.println("狗的体力值还剩:"+power+"狗与主人的亲密度:"+intimacy);
    }
    public void action(){
        Scanner sc = new Scanner(System.in);
        OUT:while(true){
            show();
            System.out.println("请选择:1,给狗狗喂食  2,与狗狗互动增加亲密度,3,不玩了");
            int choose = sc.nextInt();
            switch (choose){
                case 1://给狗狗喂食
                    feed();
                    break;
                case 2://增加亲密度
                    addIntimacy();
                    break;
                case 3://离开
                    break OUT;
                default:
                    System.out.println("输入不合法,重新选择");
                    break ;
            }


        }

    }
    public void feed(){
        System.out.println("给狗狗喂食");
        setPower(power+=7);
        if (getPower()>=100){
            setPower(100);
            System.out.println("狗狗已经吃饱了,不能在吃了,可以尝试和狗狗互动");
            show();
        }else {
            System.out.println("狗狗体力值增加7");
            show();
        }
    }
    public void addIntimacy(){
        if (getPower() >=12){
            setPower(power-12);
            setIntimacy(intimacy+5);
            if (getIntimacy() >= 100){
                setIntimacy(100);
                System.out.println("亲密度已经达到上限");
            }
            show();
        }else {
            System.out.println("狗狗饿了,请喂食!");
        }
    }

}

9,重载,继承抽象类的喂猫狗的案例

 

上一篇:黑马程序员 三、面向对象(1))


下一篇:借用 Power Apps 搭建用章申请与管理平台