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--玩游戏");    
        }
    
    }
  • 相关阅读:
    用css画三角形(提示框三角形)
    SpringMVC(2)之表单(非表单)参数绑定
    mvn jetty:run--编码GBK的不可映射字符
    Git命令及常见问题
    eclipse整合ssh框架基础
    css基础一(权重与position)
    sublime Text3下sass环境配置(windows)
    sublime Text3 设置多个浏览器预览
    初识iOS NSTimer 循环引用不释放问题
    ARC MARC 文件支持
  • 原文地址:https://www.cnblogs.com/jgjk/p/7202651.html
Copyright © 2011-2022 走看看