EntitySpriteMonster 实体实例...这么简单但不起作用?
我有这样的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这样的代码失败的唯一原因(当
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 ofwut
was loaded by a different classloader thanEntity
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 frommypackage.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?
问题解决了!
上面对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 anInventorySlot
, and not aEntity
. 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.