zoukankan      html  css  js  c++  java
  • 面向对象 宠物例子

    public class Pet {
          protected String name;// 1
          protected int sex;// 1
          protected int age;
          protected int happy;// 80
          protected int healthy;//100
          protected int hungry;//80
         
          public Pet(){} 
          public Pet(String name,int sex){
              this.name= name;
              this.sex= sex;
              this.age= 1;
              this.happy= 80;
              this.healthy= 100;
              this.hungry= 80;      
          }
         public void playGame(){
             if(!check()){
                 System.out.println("各项属性值不能为负数!");
                 return;
             }
             System.out.println("与"+this.name+"一起玩");
             this.happy+=10;
             this.healthy-=5;
             this.hungry+=12;
         }
         public void eat(){
             if(!check()){
                 System.out.println("各项属性值不能为负数!");
                 return;
             }
             System.out.println("与"+this.name+"一起吃饭");
             //this.happy+=10;
             this.healthy+=5;
             this.hungry-=20;
         } 
         public boolean check(){
             if(this.happy>0&&this.happy>0&&this.hungry>0){
                 return true;     
             } 
             if(happy<0){
                 happy=0;
             }
             if(healthy<0){
                 healthy=0;
             }
             if(hungry<0){
                 hungry=0;
             }
             return false;
         }
          
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getSex() {
            return sex;
        }
        public void setSex(int sex) {
            this.sex = sex;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public int getHappy() {
            return happy;
        }
        public void setHappy(int happy) {
            this.happy = happy;
        }
        public int getHealthy() {
            return healthy;
        }
        public void setHealthy(int healthy) {
            this.healthy = healthy;
        }
        public int getHungry() {
            return hungry;
        }
        public void setHungry(int hungry) {
            this.hungry = hungry;
        }
          
          
    
    }
    public class Cat extends Pet {
         public Cat(){}
         public Cat(String catName,int catSex){
             super(catName,catSex);
         } 
         public void showInfo(){
             System.out.println("宠物名"+this.name);
             System.out.println("宠物性别"+this.sex);
             System.out.println("宠物年龄"+this.age);
             System.out.println("开心值"+this.happy);
             System.out.println("健康值"+this.healthy);
             System.out.println("饥饿值"+this.hungry);
         }
         
    }
    import java.util.Scanner;
    
    public class Main {
        public static void mian(String[] args){
            Cat cat=new Cat("花花",1);
            
            /*cat.showInfo();
            cat.playGame();
            cat.eat();
            cat.showInfo();*/
            
            Scanner scanner=new Scanner(System.in);
            boolean flag = true;
            while(flag){
                printControl();
                String s=scanner.nextLine();
                if("1".equals(s)){
                    cat.showInfo();
                } else if("2".equals(s)){
                    cat.eat();
                } else if("3".equals(s)){
                    cat.playGame();
                } else if("bye".equals(s)){
                    flag=false;
                } else {
                    System.out.println("尚未开发!");
                }    
            }
            scanner.close();
        }
        public static void printControl(){
            System.out.println("1--显示信息");
            System.out.println("2--吃饭");
            System.out.println("3--玩游戏");    
        }
    
    }
  • 相关阅读:
    指针
    第一条:了解Objective-C语言的起源
    Java项目怎么使用Swagger生成API文档?
    springmvc 中@Controller和@RestController的区别
    Java ExecutorService四种线程池的例子与说明
    怎样解决Myeclipse内存溢出?
    过时date.toLocaleString()的解决方法
    maven中修改可用的版本
    String转换为Map
    tomcat在Debug模式下无法启动解决办法
  • 原文地址:https://www.cnblogs.com/jgjk/p/7202651.html
Copyright © 2011-2022 走看看