请你编写一个包含如下内容的程序: 1.动物园Zoo类,有area面积等数据域 2.员工类Employee类,有personName姓名、age年龄等数据域;有初始化数据域的有参构造方法。 3.Employee类有2个子类:管理员子类Manager 和 饲养员Breeder子类, 3.动物Animal类,有动物名animalName等数据域;有初始化数据域的有参构造方法 4.Animal类有:老虎Tiger、鹰Eagle、大象Elephant等子类 5.动物园中有多个员工(其中一个员工是园长)和多种动物,一个饲养员饲养多种动物。要求:main方法中创建几个不同姓名的饲养员对象,创建几个不同名字的老虎、大象等动物对象,并指定某饲养员饲养哪几个动物。
时间: 2024-03-07 15:46:51 浏览: 114
ex-zoo:创建一个动物园,里面有不同种类的动物,它们会做不同的事情
以下是实现题目要求的代码:
Animal.java
```
public class Animal {
private String animalName;
public Animal(String animalName) {
this.animalName = animalName;
}
public String getAnimalName() {
return animalName;
}
}
class Tiger extends Animal {
public Tiger(String animalName) {
super(animalName);
}
}
class Eagle extends Animal {
public Eagle(String animalName) {
super(animalName);
}
}
class Elephant extends Animal {
public Elephant(String animalName) {
super(animalName);
}
}
```
Employee.java
```
public class Employee {
private String personName;
private int age;
public Employee(String personName, int age) {
this.personName = personName;
this.age = age;
}
public String getPersonName() {
return personName;
}
public int getAge() {
return age;
}
}
class Manager extends Employee {
public Manager(String personName, int age) {
super(personName, age);
}
}
class Breeder extends Employee {
private Animal[] animals;
public Breeder(String personName, int age, Animal[] animals) {
super(personName, age);
this.animals = animals;
}
public Animal[] getAnimals() {
return animals;
}
}
```
Zoo.java
```
public class Zoo {
private double area;
private Manager manager;
private Breeder[] breeders;
private Animal[] animals;
public Zoo(double area, Manager manager, Breeder[] breeders, Animal[] animals) {
this.area = area;
this.manager = manager;
this.breeders = breeders;
this.animals = animals;
}
public double getArea() {
return area;
}
public Manager getManager() {
return manager;
}
public Breeder[] getBreeders() {
return breeders;
}
public Animal[] getAnimals() {
return animals;
}
public static void main(String[] args) {
Tiger tiger1 = new Tiger("Tony");
Tiger tiger2 = new Tiger("Jack");
Elephant elephant1 = new Elephant("Dumbo");
Elephant elephant2 = new Elephant("Manny");
Eagle eagle1 = new Eagle("Baldy");
Eagle eagle2 = new Eagle("Freedom");
Animal[] animals1 = {tiger1, elephant1};
Animal[] animals2 = {tiger2, eagle1, eagle2};
Breeder breeder1 = new Breeder("Alice", 30, animals1);
Breeder breeder2 = new Breeder("Bob", 25, animals2);
Animal[] animals = {tiger1, tiger2, elephant1, elephant2, eagle1, eagle2};
Manager manager = new Manager("Charlie", 40);
Zoo zoo = new Zoo(1000.0, manager, new Breeder[]{breeder1, breeder2}, animals);
System.out.println("Zoo area: " + zoo.getArea() + " square meters");
System.out.println("Manager: " + zoo.getManager().getPersonName());
for (Breeder breeder : zoo.getBreeders()) {
System.out.println("Breeder: " + breeder.getPersonName());
for (Animal animal : breeder.getAnimals()) {
System.out.println(breeder.getPersonName() + " takes care of " + animal.getAnimalName());
}
}
}
}
```
在main方法中,我们创建了6个动物对象,2个Breeder对象和1个Manager对象,并将这些对象加入到Zoo对象中。我们还通过Breeder对象来关联某个饲养员和他所饲养的动物。最后,我们输出了Zoo对象的一些属性以及每个饲养员所饲养的动物。
阅读全文