56
class Foo {
public void bar(int a, Object b) {
}
}
现在,我试图让“反映”从类此方法:
Class c = Foo.class;
Class[] types = { ... }; // what should be here?
Method m = c.getMethod("bar", types);
class Foo {
public void bar(int a, Object b) {
}
}
现在,我试图让“反映”从类此方法:
Class c = Foo.class;
Class[] types = { ... }; // what should be here?
Method m = c.getMethod("bar", types);
只是有一个int.class
。
Class[] types = { int.class, Object.class };
另一种方法是Integer.TYPE
。
Class[] types = { Integer.TYPE, Object.class };
其他原语也是如此。
该方法的参数是原始short
而不是对象Short
。
反射不会找到方法,因为您指定的对象很短。 getMethod()
中的参数必须完全匹配。编号: 问题发生了变化。最初的问题是找到一种只需要简单原语的方法。
你如何调用该方法?如果我想将boolean传递给这样一个方法,它不起作用:invoke(null,new Object [] {myString,myBool}); ...布尔值不能转换为Object。 – Racky 2016-11-15 08:39:13