如何生成有界变量类型的实例?

时间:2013-07-10 02:52:15

标签: java

这可以生成对象的实例吗?

 public <?> getDAO ( Class<?>DAO){

      //if using this DAO class is permitted or DAO class exists
    //we have HashMap map
return map.get(DAO);

}

或者也许是

public <T> T getDAO ( <T extends AGenericDAO> DAO){ // GenericDAO<E,I>

          //if using this DAO class is permitted or DAO class exists
        //we have HashMap map
    return map.get(DAO);

    }

1 个答案:

答案 0 :(得分:1)

听起来你想要的是:

public <T extends AGenericDAO> T getDAO(final Class<T> daoClass) {
    return daoClass.cast(map.get(daoClass));
}

其中map的类型为java.util.Map<Class<? extends AGenericDAO>, AGenericDAO>