Una semplice interfaccia:GetMethods() restituisce non hanno definito in sede di attuazione un'interfaccia generica
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
}
}
}
L'output sarà:
myMethod java.lang.String,
...//Other Method
solo myMethod viene stampato.
Ma se cambio l'interfaccia ad un generico uno:
interface Foo<T> {
void myMethod(T arg);
}
class FooImpl implements Foo<String> {
void myMethod(String arg){}
}
Poi stranamente l'uscita sarà:
myMethod java.lang.Object,
myMethod java.lang.String,
...//Other Method
Perché dopo aver cambiato l'interfaccia per un generico porterà ad un altro Metodo con un tipo di parametro Oggetto?
Guardando in "che cosa è un metodo di ponte" ora. – StarPinkER
Leggi questo tutorial, spiega tutto: http://docs.oracle.com/javase/tutorial/java/generics/erasure.html – Muel