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--玩游戏");    
        }
    
    }
  • 相关阅读:
    jython resources
    Installing a Library of Jython ScriptsPart of the WebSphere Application Server v7.x Administration Series Series
    jython好资料
    ulipad install on 64bit win7 has issue
    an oracle article in high level to descibe how to archtichre operator JAVA relevet project
    table的宽度,单元格内换行问题
    Linux常用命令大全
    dedecms系统后台登陆提示用户名密码不存在
    登录织梦后台提示用户名不存在的解决方法介绍
    Shell常用命令整理
  • 原文地址:https://www.cnblogs.com/jgjk/p/7202651.html
Copyright © 2011-2022 走看看