我正在尝试制作包含自定义类的不同子类的哈希集的哈希映射值的哈希映射,如下所示:
HashMap<String, Hashmap<String, HashSet<? extends AttackCard>>> superMap
AttackCard
有子类,例如:Mage
, Assassin
, Fighter
. superMap 中的每个 HashMap 都只会有包含AttackCard
.
当我尝试放一个
HashMap<String, HashSet<Assassin>>
以下是发生错误的代码:
public class CardPool {
private HashMap<String, HashMap<String, HashSet<? extends AttackCard>>> attackPool =
new HashMap<>();
private ArrayList<AuxiliaryCard> auxiliaryPool;
public CardPool() {
(line 24)this.attackPool.put("assassins", new AssassinPool().get());
/* this.attackPool.put("fighters", new Fighter().getPool());
this.attackPool.put("mages", new Mage().getPool());
this.attackPool.put("marksmen", new Marksman().getPool());
this.attackPool.put("supports", new Support().getPool());
this.attackPool.put("tanks", new Tank().getPool());
*/
this.auxiliaryPool = new ArrayList<>(new AuxiliaryCard().getPool());
}
这是 AssassinPool 获取方法的片段:
private HashMap<String, HashSet<Assassin>> pool = new HashMap<>();
public HashMap<String, HashSet<Assassin>> get() {
return pool;
}
我想评论一下,我可以通过使所有 AttackCardPool(例如 AssassinPool)返回并包含 AttackCard 的 HashSet 而不是它们各自的子类来轻松解决我的问题并拥有一个出色的工作程序。我试图理解这个编译错误,但是:)
compilation error at line 24: error: no suitable method found for `put(String, HashMap<String,HashSet<Assassin>>>`
this.attackPool.put("assassins", new AssassinPool(). get());
method HashMap.putp.(String, HashMap<String,HashSet<? extends AttackCard>>>` is not applicable (actual argument `HashMap<String, HashSet<Assassin>>` cannot be converted to `HashMap<String, HashSet<? extends AttackCard>>` by method invocation conversion)