本帖最后由 nanfp 于 2015-7-7 22:04 编辑
错误提示
- Exception in thread "main" java.lang.IllegalArgumentException: object is not an instance of declaring class
- 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:606)
- at classmate.TestFanShe.main(TestFanShe.java:26)
复制代码
源代码
- mport java.lang.reflect.Field;
- import java.lang.reflect.Method;
- public class TestFanShe {
- public static void main(String[] args) throws Exception {
- Class clazz = shuxing.class;
- Object ss = clazz.newInstance();
- Method[] methods = clazz.getMethods();
- for (int g = 0; g < methods.length; g++) {
- Class type = methods[g].getReturnType();
- if (type == void.class) {
- Class[] types = methods[g].getParameterTypes();
- for (Class c : types) {
- if (String.class == c) {
- System.out.println(methods[g].getName());
- methods[g].invoke("ss", "aaaaaaaa");
- }
- }
- }
- }
- }
- }
- class shuxing {
- private String s;
- public String getS() {
- return s;
- }
- public void setS(String s) {
- this.s = s;
- }
- }
复制代码 |
|