首页 > 分享 > 【0711作业】使用封装实现企鹅

【0711作业】使用封装实现企鹅

宠物类

1 package pet; 2 /** 3 * 使用封装实现宠物系统 4 * 宠物类 5 * @author L 6 * 7 */ 8 public class Pet { 9 private int health;// 健康值 10 private int love;// 亲密度 11 private String name;// 名字 12 private String sex;// 性别 13 14 public int getHealth() { 15 return health; 16 } 17 18 public void setHealth(int health) { 19 if (health < 0 || health > 100) { // "健康值应该在0至100之间,默认值为60。"; 20 this.health = 60; 21 return; 22 } 23 this.health = health; 24 } 25 26 public int getLove() { 27 return love; 28 } 29 30 public void setLove(int love) { 31 this.love = love; 32 33 } 34 35 public String getName() { 36 return name; 37 } 38 39 public void setName(String name) { 40 this.name = name; 41 } 42 43 public String getSex() { 44 return sex; 45 } 46 47 public void setSex(String sex) { 48 this.sex = sex; 49 } 50 51 public Pet() { 52 } 53 54 public Pet(int health, int love, String name, String sex) { 55 if (health < 0 || health > 100) { 56 System.out.println("健康值应该在0至100之间,默认值为60。"); 57 this.health = 60; 58 } else { 59 this.health = health; 60 } 61 this.love = 60; 62 this.name = name; 63 this.sex = sex; 64 } 65 66 // 宠物自白 67 public String showInfo() { 68 System.out.println("宠物的自白:"); 69 return "我的名字叫" + this.getName() + ",我是" + this.getSex() + ",健康值是" + this.getHealth() + ",初始亲密度为" 70 + this.getLove(); 71 } 72 }

企鹅类

1 package pet; 2 3 public class Qie extends Pet { 4 5 public Qie(int health, int love, String name, String sex) { 6 super(health, love, name, sex); 7 } 8 }

狗类

1 package pet; 2 3 public class Dog extends Pet { 4 5 public Dog(int health, int love, String name, String sex) { 6 super(health, love, name, sex); 7 } 8 9 }

测试

1 package pet; 2 3 import java.util.Scanner; 4 5 public class Main { 6 static Scanner sc=new Scanner(System.in); 7 public static void main(String[] args) { 8 String sex; 9 int love = 60; 10 System.out.println("欢迎来到宠物商店!"); 11 System.out.println("请输入要领养的宠物的名字:"); 12 String name = sc.next(); 13 System.out.print("请选择要领养的宠物类型:(1、狗狗 2、企鹅)"); 14 int choose = sc.nextInt(); 15 switch (choose) { 16 case 1: 17 System.out.println("请选择狗狗的性别:(1、雄性 2、雌性)"); 18 int choose1 = sc.nextInt(); 19 if(choose1 == 1) { 20 sex = "雄性"; 21 }else { 22 sex = "雌性"; 23 } 24 System.out.println("请输入狗狗的健康值:(0~100)"); 25 int health = sc.nextInt(); 26 System.out.println("我的亲密度初始值为60。"); 27 Dog dog = new Dog(health, love, name, sex); 28 String str = dog.showInfo(); 29 System.out.println(str); 30 break; 31 case 2: 32 System.out.println("请选择企鹅的性别:(1、Q仔 2、Q妹)"); 33 int choose2 = sc.nextInt(); 34 if(choose2 == 1) { 35 sex = "Q仔"; 36 }else { 37 sex = "Q妹"; 38 } 39 System.out.println("请输入企鹅的健康值:(0~100)"); 40 int health1 = sc.nextInt(); 41 System.out.println("我的亲密度初始值为60。"); 42 Qie Qie = new Qie(health1, love, name, sex); 43 String str1 = Qie.showInfo(); 44 System.out.println(str1); 45 break; 46 47 default: 48 System.out.println("欢迎使用宠物商店!"); 49 break; 50 } 51 } 52 }

转载于:https://www.cnblogs.com/yanglanlan/p/11171566.html

相关知识

Java中使用封装实现电子宠物系统的企鹅类正确输入健康值和亲密度
Java 创建一个企鹅类,并封装健康值和亲密度
封装
学会继承、多态、封装(1)——Java实现宠物店(领养宠物)
学会继承、多态、封装(2)——Java实现宠物店(领养宠物)
TP5:验证器的封装——5
dnf宠物装备怎么封装
访问者模式==封装??
FLV封装格式介绍及解析
1颗MiP灯珠价格=1组RGB芯片,如何实现?

网址: 【0711作业】使用封装实现企鹅 https://m.mcbbbk.com/newsview527158.html

所属分类:萌宠日常
上一篇: QQ宠物今天正式下架:烦人的企鹅
下一篇: QQ宠物永久下线,您的小企鹅被“