一个简单的界面:
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?