仅按一种类型创建多个Java对象
您可以轻松创建对象列表。请看以下示例,在该示例中,我将创建一个Employee对象数组,并在for循环中打印其详细信息。
import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; public class Tester implements Cloneable { private int data; public int getData() { return data; } public void setData(int data) { this.data = data; } public Tester(int data){ this.data = data; } public static void main(String[] args) { List<Tester> testerList = new ArrayList<Tester>(); testerList.add(new Tester(1)); testerList.add(new Tester(2)); testerList.add(new Tester(3)); testerList.add(new Tester(4)); for(Tester tester : testerList){ System.out.println(tester.getData()); } } }