[英]How to make a hashmap that contains SomeType<K,V>?
map = new HashMap<Class<? extends AbstractDAO<K,V>>, AbstractDAO<K,V>>);
is not allowed 不被允许
Is there any way to achieve something like this? 有没有办法实现这样的事情?
Like following : 如下:
class Test<K, V> {
Map<K, V> map = new HashMap<Class<? extends AbstractDAO<K,V>>, AbstractDAO<K,V>>();
}
Sure you can, as long as you declare K
and V
as types. 当然你可以,只要你宣布
K
和V
为类型。
Here I've declared them as method types: 在这里,我将它们声明为方法类型:
public static class AbstractDAO<K, V> {
}
public static <K, V> void typedMethod() {
Map<Class<? extends AbstractDAO<K, V>>, AbstractDAO<K, V>> map
= new HashMap<Class<? extends AbstractDAO<K, V>>, AbstractDAO<K, V>>();
}
Or if you don't want to declare the types, leave everything "unknown" with wildcards everywhere: 或者,如果您不想声明类型,请将所有“未知”与通配符放在一起:
public static void method() {
Map<Class<? extends AbstractDAO<?, ?>>, AbstractDAO<?, ?>> map = new HashMap<Class<? extends AbstractDAO<?, ?>>, AbstractDAO<?, ?>>();
}
All of the above code compiles. 以上所有代码都是编译的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.