I have a class B and C which both extends class A:
public class B extends A {...}
public class C extends A {...}
How can I use Java generics with a HashMap this way?
B b = new B();
C c = new C();
Map map = new HashMap();
map.put("B", b);
map.put("C", c);
Eclipse always shows an error:
The method put(String, capture#1-of ? extends A) in the type
Map is not applicable for the
arguments (String, B)
and
The method put(String, capture#1-of ? extends A) in the type
Map is not applicable for the
arguments (String, C)
解決方案
Just change the type of the HashMap to
Map map = new HashMap();