我有一个类FourByFourBoard
,它扩展了GameBoard
。我定义了以下字段:带泛型的Java集合给我提供了不适用的参数错误
private Map<Integer,List<Key<? extends GameBoard>>> mGameTypeToKeysMap = new Hashtable<Integer,List<Key<? extends GameBoard>>>();
private List<Key<FourByFourBoard>> mFourByFourBoardKeys = new ArrayList<Key<FourByFourBoard>>();
在我的构造函数中,我尝试拨打:
mGameTypeToKeysMap.put(Game.FOUR_BY_FOUR, mFourByFourBoardKeys);
但我得到这个:
在类型的方法
put(Integer, List<Key<? extends GameBoard>>)
Map<Integer,List<Key<? extends GameBoard>>>
不适用于 参数(int, List<Key<FourByFourBoard>>)
我可以使用一种不同的方法来做我想做的事情,但在盯着代码一会儿之后,我无法弄清楚为什么这种方式不起作用。
编辑
比我想的这个问题可能比较简单:
如果我尝试:
Key<GameBoard> a = mFourByFourBoardKeys.get(0);
我得到:
类型不匹配:不能转换
Key<FourByFourBoard>
至Key<GameBoard>
即使:
GameBoard someBoard = new FourByFourBoard();
是合法的。所以这仍然是一个泛型问题,但集合部分并不重要。而我的头还在旋转一点。
Key是Objectify的一个类,如果有帮助的话。 http://objectify-appengine.googlecode.com/svn/trunk/javadoc/com/googlecode/objectify/Key.html – 2012-01-08 23:29:20