public class people {
}
class friend extends people {
}
class coworkers extends people {
}
class family extends people {
}
public void fillMaps(){
ConcurrentMap<String, Collection<family>> familyNames = new ConcurrentHashMap<String, Collection<family>>();
ConcurrentMap<String, Collection<coworkers>> coworkersNames = new ConcurrentHashMap<String, Collection<coworkers>>();
ConcurrentMap<String, Collection<friend>> friendNames = new ConcurrentHashMap<String, Collection<friend>>();
populateMap(family.class, familyNames);
populateMap(coworkers.class, coworkersNames);
populateMap(friend.class, friendNames);
}
private <T> void populateMap(Class<T> clazz, ConcurrentMap<String, Collection<people>> map) {
if (clazz == family.class) {
map.put("example", new ArrayList<family>());
}
if (clazz == coworkers.class) {
map.put("example", new ArrayList<coworkers>());
}
if (clazz == friend.class) {
map.put("example", new ArrayList<friend>());
}
}
家庭、同事和朋友类都从称为人的超类扩展而来。为什么下面的方法不允许我将类用作 populateMap 方法的参数的参数。另外,为什么它不允许我在这里将子类集合作为参数传递?
error:
The method populateMap(Class<T>, ConcurrentMap<String,Collection<people>>) is not applicable for the arguments (Class<family>, ConcurrentMap<String,Collection<family>>)