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