2012-07-21 2 views
1

의 집합을 반복 나는 다음과 같은 코드가 있습니다자바 클래스

Reflections reflections = new Reflections("com.mypackage.cmds"); 
Set<Class<? extends Command>> commandClasses = reflections.getSubTypesOf(Command.class); 

그것이 무엇을하는 모든 클래스는 패키지 com.mypackage.cmds 내에 명령 클래스에서 자손의 Set에 저장하는 것입니다.

이제 반환 된 집합을 반복 또는 순회하여 각 클래스가 Class.fromName 메서드를 호출하도록로드하려고합니다. 이것을 어떻게 할 수 있습니까? 사전에

고마워,

답변

6

당신은 표준 for-each 루프를 수행

for (Class clazz: commandClasses) { 
    // operate on clazz 
} 

이것은 Iterator 인터페이스를 구현하는 모든 컬렉션 작동합니다.

0

당신은 이런 식으로 작업을 수행 할 수 있습니다

Iterator i = commandClasses.iterator(); 
     while (i.hasNext()) { 
      i.next(); //will return next object  
     }