【发布时间】:2013-08-28 11:06:07
【问题描述】:
基于这个问题Increment variable names?
我有一个数组列表“peopleHolder”,其中包含各种“人”对象。 我想基于 for 循环自动创建“人”对象。 我做了以下
peopleHolder.add(new person());
我想从 person 类中调用方法。例如person.setAge;
如何通过数组列表调用此类方法?
我想要为每个对象设置值的方法。
我看过这个答案:Java - calling methods of an object that is in ArrayList
但我认为解决方案取决于调用静态方法,并且我希望在存储对象值时使用特定于对象的方法。
【问题讨论】:
-
你的问题不清楚,什么意思?
-
我有几个人对象。我使用循环自动在“人员”arrayList 中创建新的人员对象。我想访问定义对象各种特征的方法(setAge、setHeight 等)。如何访问我在 arraylist 中创建的对象的这些方法。
-
你想在 person 类中创建 Arraylist 引用,对吗?
-
ArrayList
peopleHolder = new ArrayList ();peopleHolder.add(new Person());for(Person p:peopleHolder){p.setAge();}