試試這個:
Method[] method = cls.getClass().getDeclaredMethods();
,而不是
Method[] method = cls.getDeclaredMethods();
見這個例子:
import java.lang.reflect.Method;
public class Example {
private void one() {
}
private void two() {
}
private void three() {
}
public static void main(String[] args) {
Example program = new Example();
Class progClass = program.getClass();
// Get all the methods associated with this class.
Method[] methods = progClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println("Method " + (i + 1) + " :"
+ methods[i].toString());
}
}
}
輸出:
Method 1 :public static void Example.main(java.lang.String[])
Method 2 :private void Example.one()
Method 3 :private void Example.two()
Method 4 :private void Example.three()
這似乎是一個代理對象。如果您不想包含這些方法,則可能需要檢查方法的其他屬性。或者你使用反射來檢查實現的接口? 你怎麼得到這個課程? – rmalchow 2013-04-22 10:04:21
請包括類cls。 – javadev 2013-04-22 10:05:31
請看看我編輯的問題,認爲這是足夠的幾分服務,抱歉誤導。 – 2013-04-22 10:08:09