gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:21:03 25 4
gpt4 key购买 nike

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

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

}

最佳答案

听起来你想要的是:

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

哪里map类型为java.util.Map<Class<? extends AGenericDAO>, AGenericDAO> .

关于java - 如何生成有界变量类型的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17561723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com