是否可以遍历特定类的所有“实例”?我一直在环顾四周,并想出了:
for (Field field : SimpleCheckBox.class.getFields()) {
System.out.println(field.getName());
}
我想遍历所有字段,并在它们上实现一些功能,而不必单独选择它们。上面的代码给出了错误:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:396)
at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:525)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363)
at java.lang.Thread.run(Thread.java:679)
Caused by: java.lang.Error: Unresolved compilation problem: The method getFields() is undefined for the type Class
任何帮助,将不胜感激!