繁体   English   中英

实现Map时如何强制泛型参数超类类型?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM