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>>)