mercredi 14 septembre 2016

randomly instantiate derived Classes

I have an Animal class which is extended by several derived Classes such Monkey Dog Cat Fish etc

In my program, I want to randomly instantiate three derived animal classes, (it doesn't matter which derivation it is - they can even allow duplicates); then plug them to a list. How can I do this?

List<Animal> animalList = new ArrayList<>();

for (int i=0;i<3;i++) { Animal animal = new Dog() or new Cat or new Dog()....;

animallist.add(animal);

}




Aucun commentaire:

Enregistrer un commentaire