- import java.util.*;
- import java.lang.reflect.*;
- public class Reflector {
- public static void main(String[] args) throws Exception {
- Set<String> s = new HashSet<String>();
- s.add("foo");
- Iterator it = s.iterator();
-
- Method m = Iterator.class.getMethod("hasNext");//1
- System.out.println(m.invoke(it));
- }
- }
复制代码 为什么上面1处的语句换成Method m = it.getClass().getMethod("hasNext");就报异常呢? |