我有一個B類和C這兩種擴展A類:使用Java泛型與HashMap的
public class B extends A {...}
public class C extends A {...}
如何使用Java泛型有一個HashMap這種方式?
B b = new B();
C c = new C();
Map<String, ? extends A> map = new HashMap<String, A>();
map.put("B", b);
map.put("C", c);
Eclipse中始終顯示錯誤:
The method put(String, capture#1-of ? extends A) in the type Map is not applicable for the arguments (String, B)
和
The method put(String, capture#1-of ? extends A) in the type Map is not applicable for the arguments (String, C)
只需使用'地圖'。 –
GriffeyDog