简体   繁体   中英

How to assign multiple objects to another class/object?

I have to make a program that has a zoo with three types of animals (giraffes, tigers, and penguins) I have to make a method that assigns the animals to the zoo (there can be multiple zoos so it has to be specific) and then a method that can print all the animals in that zoo.

How do you add multiple animals to a zoo? I can assign multiple animals to a zoo (where you can print details of the animal and it shows which zoo, but I need to print details of the zoo and it shows animals using:

public class Animal extends Zoo {
    private Zoo zoo;
    public void setZoo(Zoo zoo){
        this.zoo = zoo;
    }
)

but then I can't print out all the animals inside the zoo, just the most recent animal assigned to the zoo.

This is what I have come up with that prints the most recent:

public class Zoo {
    private Animal animal;
    public void addAnimal(Animal animal){
        this.animal = animal;
    }
}

Thanks so much!!

I'll try to help, but I doubt I understood you correctly.

First, you'll need an abstract class Animal; makes no sense to inherit from Zoo (what in common they have?), and your Zoo class to hold those 3 types of animals.

All three of them should inherit from an abstract Animal because, well...they're animals and things get easier when it's time to handle array of 3 of those concrete animals.

class Zoo {
    private Animal[] animals;
    private String zooName;
    private int zooSize;

    public String getName() {
        return zooName;
    }

    public Zoo(String name, int capacity) {
        zooSize = 0;
        animals = new Animal[capacity];
        this.zooName = name;
    }
    public void addAnimal(Animal an) {
        animals[zooSize] = an;
        zooSize++;
    }

    public String toString() {
        String tempStr = "In " + zooName + ", we keep these " + zooSize + " animals:\n";
        for(int i=0; i < zooSize; ++i) {
            tempStr += animals[i].getName() + "\n";
        }
        return tempStr;
    }
}

abstract class Animal {
    private Zoo zoo;
    private String name = "";

    public Animal(String name) {
        this.name = "Animal " + name;
    }

    public String getName() {
        return name;
    }

    public void setZoo(Zoo zoo)
    {
        this.zoo = zoo;
        this.zoo.addAnimal(this);
    }

    public String belongsWhere() {
        return  name + " belongs to " +
                zoo.getName() + " zoo";
    }
}

You see, now we don't care about what type of animal Zoo keeps, it's just an Animal and it has a name and it depends of it's concrete one (Tiger, Giraffe, Penguin).

class Giraffe extends Animal {

    public Giraffe(String surname) {
        super("Giraffe " + surname);
    }
}

class Tiger extends Animal {

    public Tiger(String surname) {
        super("Tiger " + surname);
    }
}

class Penguin extends Animal {

    public Penguin(String surname) {
        super("Penguin " + surname);
    }
}

And to test it...

public class Main {

        public static void main(String[] args) {
            Zoo z1 = new Zoo("Zoo1");
            Zoo z2 = new Zoo("Zoo2");

            Animal pen1 = new Penguin("Luke");
            Animal gir1 = new Giraffe("Duke");
            Animal pen2 = new Penguin("Fluke");
            Animal tig1 = new Tiger("Brooke");

            pen1.setZoo(z1);
            pen2.setZoo(z2);
            gir1.setZoo(z2);
            tig1.setZoo(z2);

            System.out.println(pen1.belongsWhere());
            System.out.println(tig1.belongsWhere());

            System.out.println(z1); System.out.println(z2);
        }
}

I just hope I helped.

Zoo将需要包含List<Animal>List<Animal> ,而不仅仅是单个Animal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM