java面向对象,封装和继承之电子宠物应用

 1 package Text;
 2 
 3 public class Pet {
 4     protected String name;
 5     protected String sex;
 6     protected int age;
 7     protected int happy;
 8     protected int healthy;
 9     protected int hungry;
10     
11     
12     public Pet(){}
13     public Pet(String name, String sex){
14         this.name =name;
15         this.sex =sex;
16         this.age =1;
17         this.happy =80;
18         this.healthy =100;
19         this.hungry =80;
20     }
21     
22     public void playGame(){
23         if(!check()){
24             System.out.println("各项属性值不能为负数!");
25             return;
26         }
27         System.out.println("与"+this.name+"一起玩...");
28         this.happy+=10;
29         this.hungry+=10;
30     }
31     
32     public void eat(){
33         if(!check()){
34             System.out.println("各项属性值不能为负数!");
35             return;
36         }
37         System.out.println("与"+this.name+"一起吃饭...");
38         this.happy+=10;
39         this.healthy+=5;
40         this.hungry-=20;
41     }
42     
43     public boolean check(){
44         if(this.happy>0&&this.healthy>0&&this.hungry>0){
45             return true;
46         }
47         if(happy<0){
48             happy =0;
49         }
50         if(healthy<0){
51             healthy=0;
52         }
53         if(hungry<0){
54             hungry=0;
55         }
56         return false;
57     }
58     
59     
60     
61     public String getName() {
62         return name;
63     }
64     public void setName(String name) {
65         this.name = name;
66     }
67     public String getSex() {
68         return sex;
69     }
70     public void setSex(String sex) {
71         this.sex = sex;
72     }
73     public int getAge() {
74         return age;
75     }
76     public void setAge(int age) {
77         this.age = age;
78     }
79     public int getHappy() {
80         return happy;
81     }
82     public void setHappy(int happy) {
83         this.happy = happy;
84     }
85     public int getHealthy() {
86         return healthy;
87     }
88     public void setHealthy(int healthy) {
89         this.healthy = healthy;
90     }
91     public int getHungry() {
92         return hungry;
93     }
94     public void setHungry(int hungry) {
95         this.hungry = hungry;
96     }
97         
98 }
 1 package Text;
 2 
 3 public class Cat extends Pet {
 4     
 5     public Cat(){}
 6     public Cat(String catName, String catSex){
 7         super(catName, catSex);
 8     }
 9     
10     public void showInfo(){
11         System.out.println("宠物名字:" +this.name);
12         System.out.println("宠物性别:" +this.sex);
13         System.out.println("宠物年龄:" +this.age);
14         System.out.println("开心值:" +this.happy);
15         System.out.println("健康值:" +this.healthy);
16         System.out.println("饥饿值:" +this.hungry);
17         System.out.println("==========================");
18     }
19 }
 1 package Tool;
 2 
 3 import java.util.Scanner;
 4 
 5 import Text.Cat;
 6 
 7 public class Tool2 {
 8 
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         Cat a =new Cat("花花", "女");
12         
13         /*a.showInfo();
14         a.playGame();
15         a.eat();
16         a.showInfo();*/
17         
18         Scanner scanner = new Scanner(System.in);
19         boolean flag =true;
20         while(flag){
21             printControl();
22             String c =scanner.nextLine();
23             if("1".equals(c)){
24                 a.showInfo();
25             }else if("2".equals(c)){
26                 a.eat();
27             }else if("3".equals(c)){
28                 a.playGame();
29             }else if("bye".equals(c)){
30                 flag =false;
31             }else {
32                 System.out.println("功能尚未开启");;
33             }
34         }
35         scanner.close();
36     }
37     
38     public static void printControl(){
39         System.out.println("1--显示信息");
40         System.out.println("2--吃饭");
41         System.out.println("3--玩游戏");
42     }    
43 }

转载于:https://www.cnblogs.com/zym0m/p/7211430.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值