【发布时间】:2013-02-22 12:12:00
【问题描述】:
一个简单的界面:
interface Foo {
void myMethod(String arg);
}
class FooImpl implements Foo {
void myMethod(String arg){}
public static void main(String[] args) {
Class cls = FooImpl.class;
try {
for (Method method : cls.getMethods()) {
System.out.print(method.getName() + "\t");
for(Class paramCls : method.getParameterTypes()){
System.out.print(paramCls.getName() + ",");
}
System.out.println();
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
}
}
}
输出将是:
myMethod java.lang.String,
...//Other Method
只打印一个 myMethod。
但如果我将接口更改为通用接口:
interface Foo<T> {
void myMethod(T arg);
}
class FooImpl implements Foo<String> {
void myMethod(String arg){}
}
然后奇怪的是输出将是:
myMethod java.lang.Object,
myMethod java.lang.String,
...//Other Method
为什么把接口改成泛型后会导致多出一个参数类型为Object的Method?
【问题讨论】:
标签: java reflection interface