Java 泛型错误:不适用于参数

发布于 2024-11-08 12:00:09 字数 1415 浏览 0 评论 0原文

所以第一次泛型,我的任务是制作一个由正方形组成的地下城(游戏世界),这些正方形(实际上是立方体)有很多类型,但这并不重要。

所以我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

从来不烧饼 2024-11-15 12:00:09

我相信问题出在 CompositDungeon#getDimensionOf(E) 方法中的 for 循环中。

for(Subdungeon<? extends E> dungeon : getAllSubdungeons()){

...应该是...

for(Subdungeon<E> dungeon : getAllSubdungeons()){

E 已经被定义为 Square 类型的子类,所以没有必要添加 ,事实上,这是不正确的。

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.

明媚如初 2024-11-15 12:00:09

我不相信你可以像这样继承 () 它需要位于该调用之外: ;扩展了其他东西

I don't believe you can inherit like that (<something extends somethingElse>) It needs to be outside that call: <something> extends somethingElse

缺⑴份安定 2024-11-15 12:00:09

尝试这样:

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;
    }
}

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;
    }
}
云巢 2024-11-15 12:00:09

在不了解更多代码的情况下,我无法最终回答这个问题,但是......

强制 getDimension 方法的参数必须与类的泛型类型匹配的原因是什么?只能使用 getDimension(Rectangle) 而不能使用 getDimension(Square) 调用 ComlatedDungeon 是否有意义?

至少在示例中调用它的方式,看起来您确实想在与基本类型匹配的任何上调用它 - 所以,我会更改 中的原始方法Subdungeon 使用基本类型 -

public abstract class Subdungeon<E extends Square>
{
    ...
    protected abstract Dimension getDimensionOf(Square square);
    ...
}

并更改 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文