Você criou um List sem definir o tipo de objeto que essa list irá carregar. Por outro lado, inferiu isso ao ArrayList
Listhumans=newArrayList<Human>();
gaujava2
Obrigado novamente. Agora deu um erro de execução. Seguem classes atualizadas e erro.
//Human.javapackagemultiplosobjetos;importjava.lang.reflect.InvocationTargetException;importjava.util.ArrayList;importjava.util.List;publicclassHuman{privateStringname;privateintidHumano;publicHuman(){name="";}publicHuman(Stringname){this.name=name;idHumano=0;}publicHuman(IntegeridHumano){name="";this.idHumano=idHumano;}publicStringgetName(){returnthis.name;}publicvoidsetName(Stringname){this.name=name;}publicintgetidHumano(){returnidHumano;}publicvoidsetContador(intidHumano){this.idHumano=idHumano;}publicstaticList<Human>gerarHumanos(Class<?extendsHuman>clazz,intcounter){List<Human>humans=newArrayList<Human>();inti=0;//Usando construtor padraofor(i=0;i<counter&&i<1;i++){try{humans.add(clazz.newInstance());}catch(InstantiationExceptione){// TODO Auto-generated catch blocke.printStackTrace();}catch(IllegalAccessExceptione){// TODO Auto-generated catch blocke.printStackTrace();}}//Usando construtor que inicia idHumanofor(;i<counter;i++){try{humans.add(clazz.getDeclaredConstructor(Integer.class).newInstance(i));}catch(IllegalArgumentExceptione){// TODO Auto-generated catch blocke.printStackTrace();}catch(SecurityExceptione){// TODO Auto-generated catch blocke.printStackTrace();}catch(InstantiationExceptione){// TODO Auto-generated catch blocke.printStackTrace();}catch(IllegalAccessExceptione){// TODO Auto-generated catch blocke.printStackTrace();}catch(InvocationTargetExceptione){// TODO Auto-generated catch blocke.printStackTrace();}catch(NoSuchMethodExceptione){// TODO Auto-generated catch blocke.printStackTrace();}}returnhumans;}}
packagemultiplosobjetos;importjava.util.ArrayList;importjava.util.List;publicclassTestaHumanos{publicstaticvoidmain(Stringargsv[]){//Modo antigo// List<Woman> women = new ArrayList<Woman>();// List<Man> men = new ArrayList<Man>();// // // for(int i = 0; i < 100; i++)// {// women.add(new Woman());// men.add(new Man());// }//Modo usando gerador de HumanosList<Human>man=Human.gerarHumanos(Man.class,10);List<Human>woman=Human.gerarHumanos(Woman.class,10);}}