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

编程入门 行业动态 更新时间:2024-10-26 23:39:16

9,重载,继承抽象类的喂<a href=https://www.elefans.com/category/jswz/34/1769144.html style=猫狗的案例"/>

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,重载,继承抽象类的喂猫狗的案例

本文发布于:2024-02-10 15:52:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1676115.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:猫狗   案例   抽象类

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!