面向对象 宠物例子

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--玩游戏");    
    }

}

 

转载于:https://www.cnblogs.com/jgjk/p/7202651.html

好的,那我来给你举一个面向对象宠物大作战Java例子。 首先,我们定义一个宠物类Pet,包含属性name、age、gender等,以及方法speak()。 ```java public class Pet { private String name; private int age; private String gender; public Pet(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } public void speak() { System.out.println("Hello, I am a pet!"); } } ``` 然后,我们定义一个猫类Cat,继承自宠物类Pet,并添加猫特有的方法scratch()和meow()。 ```java public class Cat extends Pet { public Cat(String name, int age, String gender) { super(name, age, gender); } public void scratch() { System.out.println("I am scratching!"); } public void meow() { System.out.println("Meow!"); } } ``` 接着,我们定义一个狗类Dog,同样继承自宠物类Pet,并添加狗特有的方法bark()和fetch()。 ```java public class Dog extends Pet { public Dog(String name, int age, String gender) { super(name, age, gender); } public void bark() { System.out.println("Woof!"); } public void fetch() { System.out.println("I am fetching!"); } } ``` 最后,我们定义一个宠物大作战类Battle,包含属性pet1和pet2,分别表示两只宠物。同时,我们还定义方法start(),用于开始一场宠物大作战。 ```java public class Battle { private Pet pet1; private Pet pet2; public Battle(Pet pet1, Pet pet2) { this.pet1 = pet1; this.pet2 = pet2; } public void start() { System.out.println("The battle starts!"); pet1.speak(); pet1.scratch(); pet2.speak(); pet2.bark(); } } ``` 这样,我们就可以创建不同的宠物对象,然后进行宠物大作战了。 ```java public static void main(String[] args) { Pet cat = new Cat("Tom", 2, "male"); Pet dog = new Dog("Max", 3, "female"); Battle battle = new Battle(cat, dog); battle.start(); } ``` 输出结果如下: ``` The battle starts! Hello, I am a pet! I am scratching! Hello, I am a pet! Woof! ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值