我很难创建CodeService接口的实现类。如何将类类型用作映射键和类的值列表作为映射值?
public interface CodeService<T extends AbstractCode> {
Map<Class<T>, List<T>> getCodes();
}
AbstractCode是我的国家,城市和它在我的数据库中的相应表名性别类的抽象超类。
getCodes()
方法应该返回类型为键的映射(Country.class,City.class,Sex.class)和由数据库返回的所有国家,城市和性别的列表作为值。
我的getCodes()
方法的实现如下:
public class HibernateCodeService<T extends AbstractCode> implements CodeService<T> {
private Map<Class<T>, List<T>> map = new HashMap<Class<T>, List<T>>();
public Map<Class<T>, List<T>> getCodes() {
/* Create an instance of the HibernateCodeDao<Sex> class */
CodeDao<Sex> sexDao = new HibernateCodeDao<Sex>();
/* Set the class type as Sex.class */
Class<Sex> type = Sex.class;
/* Fetch all the sex values from the database */
List<Sex> list = sexDao.fetchAll(Sex.class);
/* Put the Sex.class as the map key, list of sexes as the value */
map.put(type, list);
/* Same code for Country and City classes */
return map;
}
}
但是,IDE的map.put(type, list)
行发出错误。
put (java.lang.Class<T>, java.util.List<T>) in Map cannot be applied to (java.lang.Class<Sex>, java.util.List<Sex>)
我的问题是,我怎么能存储类类型作为关键的IM我的地图,这样我可以获取这个类的所有值的列表?
对于这是值得的,这是一个有趣的仿制药练习,但作为一个实际操作,至少是非常有问题的。 – chrylis