[英]Java: How to call a method from class from an enumeration of instanced objects?
I made several instances of objects in a hashtable and want to call a method from each of them. 我在哈希表中创建了几个对象实例,并希望从每个对象中调用一个方法。
I made a for that goes through an enumeration of the values retrieved from said hashtable, but I'm unsure how to actually call the method on each object. 我为此做了一个从所述哈希表检索的值的枚举,但是我不确定如何在每个对象上实际调用该方法。
for(Enumeration<Agent> AgentEnum = AgentList.elements(); AgentEnum.hasMoreElements();){
//content+=
AgentEnum.nextElement();
}
Content should receive the return of the method I'm trying to call from class Agent. 内容应收到我试图从类Agent调用的方法的返回。
content = AgentEnum.nextElement().<your-method>();
Iterate over the keys in your hashmap. 遍历哈希图中的键 。 For example: 例如:
Enumeration<Agent> AgentEnum = AgentList.keys();
while(AgentEnum.hasMoreElements()) {
Agent key = AgentEnum.nextElement();
YOUR_CLASS value = AgentList.get(key);
value.whatever();
...
}
EDIT 编辑
Or use the values directly: 或直接使用这些值 :
for(YOUR_CLASS obj : AgentList.values()) {
obj.whatever();
...
}
Other methods are discussed here , for example. 例如, 此处讨论了其他方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.