![](/img/trans.png)
[英]Is there a way to force a type argument to be provided when calling a generic/parameterized method?
[英]How to force generic argument superclass type when implementing Map?
我有各种各样的GameObject
。 我想为他们所有的基本集合定义:
//Wrong number of type arguments; required 2. > expected
public interface GameObjectMap<T> extends Map<String, T extends GameObject> {
}
该集合将始终通过字符串映射(因为数据是从JSON加载的 )。 但是第二个泛型类型参数应该是GameObject
任何实例。 我不知道如何正确编写上面的代码。
你说的差不多。 只需将extends GameObject
移至第一个通用定义:
public interface GameObjectMap<T extends GameObject> extends Map<String, T> {
}
该限制来自T
的第一个声明,例如
public interface GameObjectMap<T extends GameObject> extends Map<String, T> {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.