Hallo,
hab gerade ein Problem mit Generics, und zwar hab ich folgenden Code:
[highlight=Java]public interface A {}
public class AImpl implements A {
public void onlyInAImpl() {
System.out.println("Test");
}
}
public interface B<T extends A> {
public void foo(T param);
}
public class BImpl implements B<AImpl> {
public void foo(AImpl param) {
param.onlyInAImpl();
}
}
public class Test
{
private List<A> objects;
private Map<Class<? extends A>, B<? extends A>> map;
public Test()
{
objects = new ArrayList<A>();
objects.put(new AImpl());
map = new HashMap<Class<? extends A>, B<? extends A>>();
map.put(AImpl.class, new BImpl());
}
public void fooTest()
{
for(A a : objects)
{
map.get(a.getClass()).foo(a);
}
}
}[/highlight]
In Zeile 39 bekomme ich nun folgende Fehlermeldung:
The method foo(capture#2-of ? extends A) in the type B<capture#2-of ? extends A) is not applicable for the arguments (A).
Allerdings weis ich nicht was ich dagegen machen kann, ich vermute dass es an den definierten Generics der Map liegt. Die Generic-Struktur der Interfaces A und B möchte/muss ich allerdings so beibehalten.
Bitte um Hilfe.
Danke im Voraus
Gruß André
hab gerade ein Problem mit Generics, und zwar hab ich folgenden Code:
[highlight=Java]public interface A {}
public class AImpl implements A {
public void onlyInAImpl() {
System.out.println("Test");
}
}
public interface B<T extends A> {
public void foo(T param);
}
public class BImpl implements B<AImpl> {
public void foo(AImpl param) {
param.onlyInAImpl();
}
}
public class Test
{
private List<A> objects;
private Map<Class<? extends A>, B<? extends A>> map;
public Test()
{
objects = new ArrayList<A>();
objects.put(new AImpl());
map = new HashMap<Class<? extends A>, B<? extends A>>();
map.put(AImpl.class, new BImpl());
}
public void fooTest()
{
for(A a : objects)
{
map.get(a.getClass()).foo(a);
}
}
}[/highlight]
In Zeile 39 bekomme ich nun folgende Fehlermeldung:
The method foo(capture#2-of ? extends A) in the type B<capture#2-of ? extends A) is not applicable for the arguments (A).
Allerdings weis ich nicht was ich dagegen machen kann, ich vermute dass es an den definierten Generics der Map liegt. Die Generic-Struktur der Interfaces A und B möchte/muss ich allerdings so beibehalten.
Bitte um Hilfe.
Danke im Voraus
Gruß André
Zuletzt bearbeitet: