public class People {
class Family extends People {
}
}
public class Together {
private static Collection<Family> familyList = new ArrayList<Family>();
private static ConcurrentMap<String, Collection<People>> registry = new ConcurrentHashMap<String, Collection<People>>();
static {
registry.put(Family.class.toString(), familyList);
}
}
错误信息:
The method put(String, Collection<people>) in the type Map<String,Collection<people>> is not applicable for the arguments (String, Collection<family>)
为什么我放不familyList
进去registry
?我认为由于family
扩展people
了我应该能够将子类型放入超类型registry
中。
编辑:以上已解决。我的问题的最后一部分涉及使用相同名称的更复杂的示例:
public class Together {
private static ConcurrentMap<String, Collection<Family>> familyMap= new ConcurrentHashMap<String, Collection<Family>>();
private static ConcurrentMap<String, ConcurrentMap<String, Collection<People>>> registry2 = new ConcurrentHashMap<String, ConcurrentMap<String, Collection<People>>>();
static {
registry2.put(Family.class.toString(), familyMap);
}
}
(我已经尝试将声明更改registry2
为拥有?extends People
现在错误是:
The method put(String, ConcurrentMap<String,Collection<People>>) in the type Map<String,ConcurrentMap<String,Collection<People>>> is not applicable for the arguments (String, ConcurrentMap<String,Collection<Family>>)