EntitySpriteMonster 实体实例...这么简单但不起作用?

发布于 2024-11-19 04:28:15 字数 1097 浏览 4 评论 0原文

我有这样的代码:

class Entity;

class EntityTool extends Entity;
class EntitySprite extends Entity;

class EntityToolSpoon extends EntityTool;
class EntityToolBow extends EntityTool;
class EntitySpritePlayer extends EntitySprite;
class EntitySpriteMonster extends EntitySprite;

现在我在 Entity 中有一个名为 move() 的方法,其他一些方法会覆盖它。

现在,如果参数是 EntitySpriteMonster,为什么这不起作用:

public void foonction(Object wut)
{
    if (wut instanceof Entity) ((Entity)wut).move(x,y);
}

移动函数(尽管这是多余且不需要的。它在 if rawpeek instanceof... 处失败) :

@Override
public void move(double x, double y) {
    super.move(x, y);
    for (int i=0; i<8; i++) {
        Object rawpeek = palette.get(i);
        System.out.println(rawpeek);
        if (rawpeek == null) continue;
        if (rawpeek instanceof Entity)
            ((Entity) rawpeek).move(x, y);
    }
}

编辑:它只是中止。 (operator instanceof 返回 false)

Eclipse 在调试中向我展示,wut 绝对是一个 EntitySpriteMonster

I have code like:

class Entity;

class EntityTool extends Entity;
class EntitySprite extends Entity;

class EntityToolSpoon extends EntityTool;
class EntityToolBow extends EntityTool;
class EntitySpritePlayer extends EntitySprite;
class EntitySpriteMonster extends EntitySprite;

Now I have a method called move() in Entity, and some others overwrite it.

Now, if the argument is an EntitySpriteMonster, why does this not work:

public void foonction(Object wut)
{
    if (wut instanceof Entity) ((Entity)wut).move(x,y);
}

The move function (though this is redundant and unneeded. it fails at if rawpeek instanceof...):

@Override
public void move(double x, double y) {
    super.move(x, y);
    for (int i=0; i<8; i++) {
        Object rawpeek = palette.get(i);
        System.out.println(rawpeek);
        if (rawpeek == null) continue;
        if (rawpeek instanceof Entity)
            ((Entity) rawpeek).move(x, y);
    }
}

EDIT: It just aborts. (operator instanceof returns false)

Eclipse shows me in debug, that wut is definetly an EntitySpriteMonster.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

新雨望断虹 2024-11-26 04:28:15

我知道这样的代码失败的唯一原因(当 wut 已被验证为正确类型时)是 wut 的类是由不同的加载的类加载器比类中的 Entity 更重要。

Java 中的类由其包名称、简单名称和加载它的类加载器来标识。因此,mypackage.Entity 可能mypackage.Entity 如果它们是由单独的类加载器加载的不同。

这通常仅在您使用某种插件机制(OSGi,...)时才会发生。你用这样的东西吗?

The only reason that I know of that code like this fails (when wut has been verified to be of the correct type) is that the class of wut was loaded by a different classloader than Entity as in your class.

A class in Java is identified by it's package name, it's simple name and the classloader that loaded it. So mypackage.Entity might be different from mypackage.Entity if they have been loaded by separate classloaders.

This usually only happens when you use some kind of plug-in mechanism (OSGi, ...). Do you use something like this?

篱下浅笙歌 2024-11-26 04:28:15

问题解决了!

上面对instanceof的使用是正确的。
问题是:

((Inventory)palette).get(index) 返回一个 InventorySlot,而不是 Entity。 InventorySlot 包含 Entites,所以我错过了从容器中查看第一个项目。

我的正确代码应该是 Object rawpeek = paltte.get(i).peek();

感谢大家的尝试。对于这个明显的问题,我们深表歉意。

Problem solved!

The above use of instanceof was correct.
The problem instead was:

((Inventory)palette).get(index) returned an InventorySlot, and not a Entity. InventorySlot contains Entites, so I was missing to peek the first item from the container.

My correct code should be Object rawpeek = paltte.get(i).peek();

Thanks everyone for trying. And sorry for this obvious problem.

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