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 w开发者_Python百科hich 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.
I believe the issue is in your for loop in ComposedDungeon#getDimensionOf(E) method.
for(Subdungeon<? extends E> dungeon : getAllSubdungeons()){
...should be...
for(Subdungeon<E> dungeon : getAllSubdungeons()){
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:
public class Subdungeon<E>
{
protected abstract Dimension getDimension(E square);
}
public class ComposedDungeon<E> extends Subdungeon<E>
{
protected Dimension getDimension(E square)
{
Dimension dimension;
// put your stuff here.
return dimension;
}
}
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 a ComposedDungeon<Rectangle>
should only be able to be called with getDimension(Rectangle)
and not getDimension(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 -
public abstract class Subdungeon<E extends Square>
{
...
protected abstract Dimension getDimensionOf(Square square);
...
}
And change ComposedDungeon
to match.
精彩评论