4

クラスAにメソッドがあります:

class Parameter {
...
}

class A {
   private <T extends B> void call(T object, Parameter... parameters){
   ...
   }
}

ここで、リフレクションを使用してメソッド「call」を取得したいのですが、

A a = new A();
// My question is what should be arguments in getDeclaredMethod
//Method method = a.getClass().getDeclaredMethod()

どうも。

4

1 に答える 1

6

これらはBandParameter[]である必要があります。Bは と の消去であるためT、varargs は配列として実装されます。

Method method = a.getClass().getDeclaredMethod(
        "call",
        B.class,
        Parameter[].class
);

構文エラーがあることに注意してください: <T extends of B>should be <T extends B>.

また、あなたが示しているメソッドは、ジェネリックである必要はまったくないことに注意してください。これも同様に機能します。

private void call(B object, Parameter... parameters) { ... }
于 2013-08-24T04:47:10.380 に答える