使用继承优化电子宠物系统

 package package com.sky.oop;
  /**
   * 父类 :宠物类
   *
   */
  public class Pet {
     private String name; // 宠物名字
     private int health; // 宠物健康值
     private int love; // 宠物亲密度
 
     public String getName() {
         return name;
     }
 
     public void setName(String name) {
        this.name = name;
     }
 
     public int getHealth() {
         return health;
     }
 
     public Pet() {
     }
 
     public Pet(String name, int health, int love) {
         this.name = name;
        this.health = health;
         this.love = love;
     }
 
     public void setHealth(int health) {
         if (health < 0 || health > 100) {
             System.out.println("健康值应该在0至100之间,默认值为60。");
             this.health = 60;
             return;
         }
         this.health = health;
     }
 
     public int getLove() {
         return love;
     }
 
     public void setLove(int love) {
         if (love < 0 || love > 100) {
             System.out.println("亲密度应该在0至100之间,默认值为60。");
            this.love = 60;
            return;
         }
        this.love = love;
    }
 
     //宠物自白方法
    public void print() {
       System.out.println("宠物的自白:\n我的名字叫" + name + ",健康值是" + health + ",和主人亲密度是" + love);
     }
 }

---------------------------------------------------------------------------------------------------------------------------------

 package package com.sky.oop;
  /**
    * 子类:狗狗类
   *
   */
  public class Dog extends Pet {
      private String strain;  //狗狗品种
     
    public String getStrain() {
        return strain;
    }

    public void setStrain(String strain) {
         this.strain = strain;
     }
 
     public Dog(){}
     
     public Dog(String name,int health,int love,String strain) {
         super(name, health, love);
         this.strain = strain;
    }
    
     //重写父类宠物自白方法 
    public void print() {    
        super.print();
         System.out.println("我是一只"+strain+"。");
     }
 }

--------------------------------------------------------------------------------------------

  package com.szxs.pet;

 /**
  * 子类:企鹅类
  *
  */
  public class Penguin extends Pet {
      private String sex; //性别
     
    public String getSex() {
         return sex;
    }
 
    public void setSex(String sex) {
        this.sex = sex;
     }
     
     public Penguin() {}

    public Penguin(String name, int health, int love,String sex) {
       super(name, health, love);
        this.sex=sex;
     }

    //重写父类宠物自白方法
     public void print() {
       super.print();
         System.out.println("我的性别是"+sex+"。");
     }
 }

--------------------------------------------------------------------------------------------------------------------

  package com.sky.oop;
  
 public class TestPet {
 
     public static void main(String[] args) {
         Dog dog=new Dog("乐乐",75,35,"德牧");
         Penguin pg=new Penguin("楠楠",100,70,"Q仔");
         dog.print();
        pg.print();
    }

 }
  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于电子宠物这样的应用程序,可以考虑使用面向对象编程中的继承优化代码。 首先,定义一个基类Pet,包含宠物的一些通用属性和方法,比如名字、年龄、体重、喂食、运动等。 然后,可以定义不同种类的宠物,比如Dog、Cat、Fish等,它们都继承自Pet类,并可以添加自己特有的属性和方法,比如不同种类的宠物可以有不同的叫声、喜欢的玩具等。 在实现继承时,可以使用Java中的extends关键字,如下所示: ```java public class Pet { protected String name; protected int age; protected double weight; public void feed() { System.out.println("Feeding " + name + "..."); } public void play() { System.out.println("Playing with " + name + "..."); } } public class Dog extends Pet { private String barkSound; public Dog(String name, int age, double weight, String barkSound) { this.name = name; this.age = age; this.weight = weight; this.barkSound = barkSound; } public void bark() { System.out.println(name + " barks: " + barkSound); } } public class Cat extends Pet { private String meowSound; public Cat(String name, int age, double weight, String meowSound) { this.name = name; this.age = age; this.weight = weight; this.meowSound = meowSound; } public void meow() { System.out.println(name + " meows: " + meowSound); } } public class Fish extends Pet { private String swimStyle; public Fish(String name, int age, double weight, String swimStyle) { this.name = name; this.age = age; this.weight = weight; this.swimStyle = swimStyle; } public void swim() { System.out.println(name + " swims: " + swimStyle); } } ``` 这样,我们可以创建不同种类的宠物对象,并对其进行操作: ```java public class Main { public static void main(String[] args) { Dog dog = new Dog("Buddy", 2, 10.5, "Woof woof!"); Cat cat = new Cat("Fluffy", 3, 5.8, "Meow meow!"); Fish fish = new Fish("Goldie", 1, 0.3, "Freestyle"); dog.feed(); cat.play(); fish.swim(); dog.bark(); cat.meow(); } } ``` 这样的设计可以使代码更加清晰、易于维护和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值