实现电子宠物系统可以使用Java多态的特性,以下是完整攻略:
一、电子宠物系统的基本要求
电子宠物系统是模拟一个宠物的生命周期,包括喂食、玩耍、睡觉、生病等多种状态。系统需要实现以下功能:
- 宠物属性:宠物的名字、体力、饥饿值等属性;
- 宠物动作:宠物可以吃食物、玩耍、睡觉、生病、死亡等;
- 宠物状态:宠物会根据不同的状态进行不同的动作,例如当它饥饿时就会吃食物。
二、实现电子宠物系统的代码设计
- Pet类:宠物类,包含宠物的基本属性和方法,例如名字、体力、饥饿值、吃食物、玩耍、睡觉等。
public abstract class Pet {
private String name;//名字
private int health;//体力
private int hunger;//饥饿值
public Pet(String name, int health, int hunger) {
this.name = name;
this.health = health;
this.hunger = hunger;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getHunger() {
return hunger;
}
public void setHunger(int hunger) {
this.hunger = hunger;
}
public abstract void eat();//吃食物
public abstract void play();//玩耍
public abstract void sleep();//睡觉
}
- Animal类:动物类,是Pet类的子类,包含动物的特有属性和方法。
public class Animal extends Pet {
private String species;//种类
public Animal(String name, String species, int health, int hunger) {
super(name, health, hunger);
this.species = species;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
@Override
public void eat() {//重写吃食物方法
System.out.println(this.getSpecies() + this.getName() + " 爱吃肉!");
this.setHunger(this.getHunger() + 10);
this.setHealth(this.getHealth() + 5);
}
@Override
public void play() {//重写玩耍方法
System.out.println(this.getSpecies() + this.getName() + " 喜欢找东西玩!");
this.setHunger(this.getHunger() - 5);
this.setHealth(this.getHealth() - 5);
}
@Override
public void sleep() {//重写睡觉方法
System.out.println(this.getSpecies() + this.getName() + " 睡得很香!");
this.setHunger(this.getHunger() - 10);
this.setHealth(this.getHealth() + 10);
}
}
- PetFactory类:宠物工厂类,用来创建不同种类的宠物。
public class PetFactory {
public static Pet createPet(String name, String species, int health, int hunger) {
switch (species) {
case "dog":
return new Dog(name, health, hunger);
case "cat":
return new Cat(name, health, hunger);
default:
return null;
}
}
}
- Dog类和Cat类:动物类的子类,分别表示狗和猫两种不同种类的动物。
public class Dog extends Animal {
public Dog(String name, int health, int hunger) {
super(name, "Dog", health, hunger);
}
//重写父类的方法
@Override
public void eat() {
System.out.println("Dog " + this.getName() + " eat bone!");
this.setHunger(this.getHunger() + 10);
this.setHealth(this.getHealth() + 5);
}
@Override
public void play() {
System.out.println("Dog " + this.getName() + " like ball!");
this.setHunger(this.getHunger() - 5);
this.setHealth(this.getHealth() - 5);
}
@Override
public void sleep() {
System.out.println("Dog " + this.getName() + " sleep good!");
this.setHunger(this.getHunger() - 10);
this.setHealth(this.getHealth() + 10);
}
}
public class Cat extends Animal {
public Cat(String name, int health, int hunger) {
super(name, "Cat", health, hunger);
}
//重写父类的方法
@Override
public void eat() {
System.out.println("Cat " + this.getName() + " eat fish!");
this.setHunger(this.getHunger() + 10);
this.setHealth(this.getHealth() + 5);
}
@Override
public void play() {
System.out.println("Cat " + this.getName() + " like toy!");
this.setHunger(this.getHunger() - 5);
this.setHealth(this.getHealth() - 5);
}
@Override
public void sleep() {
System.out.println("Cat " + this.getName() + " sleep sweetly!");
this.setHunger(this.getHunger() - 10);
this.setHealth(this.getHealth() + 10);
}
}
三、示例说明
以下是两个示例来说明如何使用电子宠物系统:
示例一:创建狗和猫作为宠物进行游戏
Pet dog = PetFactory.createPet("小黑", "dog", 50, 50);
Pet cat = PetFactory.createPet("小虎", "cat", 50, 50);
dog.eat();
cat.play();
dog.sleep();
cat.eat();
dog.play();
cat.sleep();
输出结果:
Dog 小黑 eat bone!
Cat 小虎 like toy!
Dog 小黑 sleep good!
Cat 小虎 eat fish!
Dog 小黑 like ball!
Cat 小虎 sleep sweetly!
示例二:创建动物园,添加不同种类的动物宠物
List<Pet> pets = new ArrayList<>();
pets.add(PetFactory.createPet("小兔", "rabbit", 50, 50));//不存在的种类,返回null
pets.add(PetFactory.createPet("小白", "dog", 50, 50));
pets.add(PetFactory.createPet("小花", "cat", 50, 50));
pets.add(PetFactory.createPet("小黑", "dog", 50, 50));
pets.add(PetFactory.createPet("小黄", "cat", 50, 50));
for (Pet pet : pets) {
if (pet != null) {
System.out.println(pet.getSpecies() + " " + pet.getName());
}
}
输出结果:
Dog 小白
Cat 小花
Dog 小黑
Cat 小黄
以上就是电子宠物系统的完整攻略,使用Java多态可以很好地实现宠物多种状态的模拟并且扩展性良好。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java多态实现电子宠物系统 - Python技术站