引自免费微信小程序:皆我百晓生
为了构建这个“模拟动物园管理系统”,我们首先从定义基础类和接口开始。下面是具体的代码实现:
// 定义动物的基本属性和行为
abstract class Animal {
private String name;
private int age;
private double health;
public Animal(String name, int age, double health) {
this.name = name;
this.age = age;
this.health = health;
}
public abstract void makeSound();
public abstract void eat();
public abstract void sleep();
}
// 移动接口
interface Movable {
void move();
}
// 呼吸接口
interface Breathable {
void breathe();
}
// 实现狮子类,继承Animal,实现Movable和Breathable
class Lion extends Animal implements Movable, Breathable {
public Lion(String name, int age, double health) {
super(name, age, health);
}
@Override
public void makeSound() {
System.out.println("Roar");
}
@Override
public void eat() {
System.out.println("Eating meat");
}
@Override
public void sleep() {
System.out.println("Sleeping");
}
@Override
public void move() {
System.out.println("Walking");
}
@Override
public void breathe() {
System.out.println("Breathing with lungs");
}
}
// 实现鱼的类
class Fish extends Animal implements Breathable {
public Fish(String name, int age, double health) {
super(name, age, health);
}
@Override
public void makeSound() {
System.out.println("Blub");
}
@Override
public void eat() {
System.out.println("Eating algae");
}
@Override
public void sleep() {
System.out.println("Resting");
}
@Override
public void breathe() {
System.out.println("Breathing with gills");
}
}
// 实现鸟的类
class Bird extends Animal implements Movable, Breathable {
public Bird(String name, int age, double health) {
super(name, age, health);
}
@Override
public void makeSound() {
System.out.println("Chirp");
}
@Override
public void eat() {
System.out.println("Pecking seeds");
}
@Override
public void sleep() {
System.out.println("Napping");
}
@Override
public void move() {
System.out.println("Flying");
}
@Override
public void breathe() {
System.out.println("Breathing with lungs");
}
}
// 动物园管理系统类
class Zoo {
private List<Animal> animals;
public Zoo() {
this.animals = new ArrayList<>();
}
public void addAnimal(Animal animal) {
animals.add(animal);
}
public void removeAnimal(Animal animal) {
animals.remove(animal);
}
public void feedAll() {
for (Animal animal : animals) {
animal.eat();
}
}
public void putAllToSleep() {
for (Animal animal : animals) {
animal.sleep();
}
}
public void makeAllSounds() {
for (Animal animal : animals) {
animal.makeSound();
}
}
public void moveAllAnimals() {
for (Animal animal : animals) {
if (animal instanceof Movable) {
((Movable) animal).move();
}
}
}
}
// 主类和主方法
public class ZooApp {
public static void main(String[] args) {
Zoo zoo = new Zoo();
// 添加不同类型的动物
zoo.addAnimal(new Lion("Simba", 5, 90));
zoo.addAnimal(new Fish("Nemo", 1, 75));
zoo.addAnimal(new Bird("Polly", 3, 80));
// 执行一些管理操作
zoo.feedAll();
zoo.putAllToSleep();
zoo.makeAllSounds();
zoo.moveAllAnimals();
}
}
这段代码涵盖了基本的类和接口定义,以及一个简单的动物园管理系统的实现。通过ZooApp类中的main方法,我们可以创建动物园对象,添加不同的动物,并执行各种管理操作,如喂养所有动物、让它们睡觉、发出声音以及移动(仅限于实现了Movable接口的动物)。
对于扩展功能,例如添加更多的动物类型或员工管理,以及实现GUI,这些都可以在此基础上进行开发。不过,具体实现细节会根据具体需求和设计模式有所不同,这里就不一一赘述了。希望这个基础框架能帮助你启动你的项目!