java封装-猫狗宠物店案例
2021/8/25 9:06:00
本文主要是介绍java封装-猫狗宠物店案例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
宠物类(父类):
public class Pet { private String name; private int health; private int love; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getHealth() { return health; } public void setHealth(int health) { if (health < 0 || health > 100) { System.out.println("您输入有误!已设置为默认值!"); health = 60; } this.health = health; } public int getLove() { return love; } public void setLove(int love) { if (love < 0 || love > 100) { System.out.println("您输入有误!已设置为默认值!"); love = 60; } this.love = love; } public void showInfo() { System.out.println("名:" + name); System.out.println("健康值:" + health); System.out.println("亲密度:" + love); } public Pet() { super(); } public Pet(String name, int health, int love) { super(); this.setHealth(health); this.setName(name); this.setLove(love); } }
猫类(子类):
public class Cat extends Pet{ private String gender; public String getgender() { return gender; } public void setgender(String gender) { this.gender = gender; } @Override public void showInfo() { System.out.println("名:" + super.getName()); System.out.println("健康值:" + super.getHealth()); System.out.println("亲密度:" + super.getLove()); System.out.println("性别:" + gender); } public Cat() { super(); } public Cat(String gender, String name, int health, int love) { super(name, health, love); this.setgender(gender); } }
狗类(子类):
public class Dog extends Pet{ private String breed; public String getBreed() { return breed; } public void setBreed(String breed) { this.breed = breed; } public Dog() { super(); } public Dog(String name, int health, int love, String breed) { super(name, health, love); this.breed = breed; } @Override public void showInfo() { super.showInfo(); System.out.println("品种:" + breed); } public Dog(String name, int health, int love) { this(name, health, love, null); } }
测试类:
public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("欢迎来到宠物店"); System.out.println("请选择:1.狗2.猫"); String choice = input.next(); System.out.println("请输入宠物的名字:"); String name = input.next(); System.out.println("请输入宠物健康值:"); int health = input.nextInt(); System.out.println("请输入宠物亲密度:"); int love = input.nextInt(); switch (choice) { case "1": System.out.println("请输入狗的品种:1.柴犬2.拉布拉多"); int breed = input.nextInt(); // Dog dog = new Dog(name, health, love, breed == 1 ? "柴犬" : "拉布拉多"); // dog.showInfo(); Dog dog1 = new Dog(name, health, love); dog1.setBreed(breed == 1 ? "柴犬" : "拉布拉多"); dog1.showInfo(); break; case "2": System.out.println("请输入猫的性别:1.公2.母"); int gender = input.nextInt(); new Cat(gender == 1 ? "公" : "母", name, health, love).showInfo(); break; default: System.out.println("输入错误!"); break; } }
这篇关于java封装-猫狗宠物店案例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?