use*_*555 4 java generics reflection
我在A班有一个方法:
class Parameter {
...
}
class A {
private <T extends B> void call(T object, Parameter... parameters){
...
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想使用反射来获取方法"call",
A a = new A();
// My question is what should be arguments in getDeclaredMethod
//Method method = a.getClass().getDeclaredMethod()
Run Code Online (Sandbox Code Playgroud)
谢谢.
他们应该是B和Parameter[],因为B是擦除的T和可变参数作为数组实现的:
Method method = a.getClass().getDeclaredMethod(
"call",
B.class,
Parameter[].class
);
Run Code Online (Sandbox Code Playgroud)
请注意,您有语法错误:<T extends of B>应该是<T extends B>.
另请注意,您显示它的方法根本不需要是通用的.这也可以:
private void call(B object, Parameter... parameters) { ... }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2710 次 |
| 最近记录: |