Java 泛型错误:不适用于参数
所以第一次泛型,我的任务是制作一个由正方形组成的地下城(游戏世界),这些正方形(实际上是立方体)有很多类型,但这并不重要。
所以我有一个CompositedDungeons
类,这个类代表一个由其他地下城构建的地下城,它没有自己的方块,但包含SubDungeon
类的其他子级。通过这种方式,我得到了一个类似树的结构,其根为 CompositedDungeon
,而叶子不能有自己的叶子,除非它们也是 ComlatedDungeons
。
第一题(超级级)
public abstract class Subdungeon<E extends Square> {
....
问题方法:
protected abstract Dimension getDimensionOf(E square);
第二题:
public class ComposedDungeon<E extends Square> extends Subdungeon<E> {
/**
* Return the dimension of the given Square.
*
* @param square
* The square of which the dimension is required.
* @return The dimension which contains this square.
*/
protected Dimension getDimensionOf(E square){
for(Subdungeon<? extends E> dungeon : getAllSubdungeons()){
Dimension dimension = dungeon.getDimensionOf(square);
if(dimension != null)
return dimension.add(getDimensionOfDungeon(dungeon));
}
return null;
}
错误 - 类型 Subdungeon< 中的方法 getDimensionOf( ? extends E) ?延伸E>不适用于参数 (E)
我不知道如何解决这个问题,这个想法是使该方法递归,这样它就会一直搜索,直到找到一个不是CompositedDungeon
的叶子>....
我希望有人能理解并提供帮助。
So first time generics, my assignment is to make a dungeon(game world) made out of squares, these squares(actually cubes) have a lot of types but this is not real important.
So i have a class of ComposedDungeons
, this class represents a dungeon built out of other dungeons, it does not have squares of its own but contains other childs of the SubDungeon
class. This way I get a tree like structure with a root ComposedDungeon
and leaves that can not have leaves of there own except if they are also ComposedDungeons
.
THE FIRST ONE(SUPER class)
public abstract class Subdungeon<E extends Square> {
....
ProblemMethod:
protected abstract Dimension getDimensionOf(E square);
THE SECOND ONE:
public class ComposedDungeon<E extends Square> extends Subdungeon<E> {
/**
* Return the dimension of the given Square.
*
* @param square
* The square of which the dimension is required.
* @return The dimension which contains this square.
*/
protected Dimension getDimensionOf(E square){
for(Subdungeon<? extends E> dungeon : getAllSubdungeons()){
Dimension dimension = dungeon.getDimensionOf(square);
if(dimension != null)
return dimension.add(getDimensionOfDungeon(dungeon));
}
return null;
}
error
- The method getDimensionOf( ? extends E) in the type Subdungeon< ? extends E> is not applicable for the arguments (E)
I am out of ideas of how to fix this, the idea was to make the method recursive so it would stay searching until it would found a leaf which is not a ComposedDungeon
....
I hope someone gets it and can help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信问题出在 CompositDungeon#getDimensionOf(E) 方法中的 for 循环中。
...应该是...
E 已经被定义为 Square 类型的子类,所以没有必要添加 ,事实上,这是不正确的。
I believe the issue is in your for loop in ComposedDungeon#getDimensionOf(E) method.
...should be...
E is already defined to be a sub class of the type
Square
, so it is not necessary to add the , as a matter of fact, it is incorrect.我不相信你可以像这样继承 (;扩展了其他东西
) 它需要位于该调用之外:I don't believe you can inherit like that (
<something extends somethingElse>
) It needs to be outside that call:<something> extends somethingElse
尝试这样:
try it like this:
在不了解更多代码的情况下,我无法最终回答这个问题,但是......
强制
getDimension
方法的参数必须与类的泛型类型匹配的原因是什么?只能使用getDimension(Rectangle)
而不能使用getDimension(Square)
调用ComlatedDungeon
是否有意义?至少在示例中调用它的方式,看起来您确实想在与基本类型匹配的任何上调用它 - 所以,我会更改
中的原始方法Subdungeon
使用基本类型 -并更改
ComlatedDungeon
以匹配。I can't conclusively answer this without knowing more about the code, but...
What's the reasoning behind enforcing that the
getDimension
method's parameter must match the generic type of the class? Does it make sense that aComposedDungeon<Rectangle>
should only be able to be called withgetDimension(Rectangle)
and notgetDimension(Square)
?At least the way you're calling it in the example, it looks like you really want to call it on anything that matches the base type - so, I'd change the original method in
Subdungeon
to use the base type -And change
ComposedDungeon
to match.