I create school project in Greenfoot and I try creating one method which has two parameters, one for numbers of instances and second for a name of a class from which I want to create instances. Here is the concrete code:
private <T extends Actor> void manyActors(int many, Class<T> cls)
{
try
{
for(int i=0;i<many;i++)
{
addObjectRnd( cls.newInstance() );
}
}
catch(InstantiationException e)
{
some not important code
}
catch(IllegalAccessException e){
...
}
I think this is not a good solution but I can't figure out how this problem solve in another way and possibly better. exist different and possibly better solution or only another option is it using a structure like array or List?
Aucun commentaire:
Enregistrer un commentaire