领养宠物

父类:

 1 package com.Pet;
 2 
 3 public class Pet {
 4     protected String name;
 5     protected String sex;
 6     protected int heathy;
 7     protected int qinMi;
 8     public String getName() {
 9         return name;
10     }
11     public void setName(String name) {
12         this.name = name;
13     }
14     public String getSex() {
15         return sex;
16     }
17     public void setSex(String sex) {
18         if(sex.equals("1")) {
19             this.sex = "公仔";
20         }else {
21             this.sex = "母仔";
22         }
23     }
24     public int getHeathy() {
25         return heathy;
26     }
27     public void setHeathy(int heathy) {
28         if(heathy<0||heathy>100) {
29             heathy = 60;
30             System.out.println("健康值应该在0~100之间,默认为60");
31         }
32         this.heathy = heathy;
33     }
34     public int getQinMi() {
35         return qinMi;
36     }
37     public void setQinMi(int qinMi) {
38         if(qinMi<0||qinMi>100) {
39             qinMi = 61;
40             System.out.println("亲密度应该在0~100之间,默认值为61");
41         }
42         this.qinMi = qinMi;
43     }
44     
45 }

子类(狗类):

1 package com.Pet;
2 
3 public class Dog extends Pet{
4     public void showInfo() {
5         System.out.println("狗狗的自白:");
6         System.out.println("我的名字叫:"+this.getName()+","+"健康值是:"+this.getHeathy()+","+"和主人亲密度为:"+this.getQinMi()+","+"我的性别为:"+this.getSex()); 
7     }
8 }

子类(企鹅类):

1 package com.Pet;
2 
3 public class Penguin extends Pet{
4     public void showInfo() {
5         System.out.println("企鹅的自白:");
6         System.out.println("我的名字叫:"+this.getName()+","+"健康值是:"+this.getHeathy()+","+"和主人亲密度为:"+this.getQinMi()+","+"我的性别为:"+this.getSex());
7     }
8 }

测试类:

 1 package com.Pet;
 2 
 3 import java.util.Scanner;
 4 
 5 public class TestPet {
 6     public static void main(String[] args) {
 7         System.out.println("欢迎来到宠物店!");
 8         Scanner input = new Scanner(System.in);
 9         Dog dog =new Dog();
10         Penguin qiE = new Penguin();
11         System.out.println("请选择您想要的宠物:1.狗狗/2.企鹅");
12         int choose = input.nextInt();
13         
14         switch (choose) {
15         case 1:
16             System.out.println("请输入要领养宠物的名字:");
17             dog.setName(input.next());
18             System.out.println("请选择宠物性别:1.公/2.母");
19             dog.setSex(input.next());
20             System.out.println("请输入健康值:");
21             dog.setHeathy(input.nextInt());
22             System.out.println("请输入亲密度:");
23             dog.setQinMi(input.nextInt());
24             dog.showInfo();
25             break;
26             
27         case 2:
28             System.out.println("请输入要领养宠物的名字:");
29             qiE.setName(input.next());
30             System.out.println("请选择宠物性别:1.公/2.母");
31             qiE.setSex(input.next());
32             System.out.println("请输入健康值:");
33             qiE.setHeathy(input.nextInt());
34             System.out.println("请输入亲密度:");
35             qiE.setQinMi(input.nextInt());
36             qiE.showInfo();
37             break;
38 
39         default:
40             break;
41         }
42         
43     }
44 }

输出:

原文地址:https://www.cnblogs.com/Zhangchuanfeng1/p/10281204.html