在 Java 中删除对象时出现问题
让我们说一个图形世界,让我们说一个API < em>World 和 Actor,并且我从 Actor 固有的新类名 Food 构建了一个对象,但在某些情况下我需要这个物体从我的世界中消失。 这样做的好方法应该是什么?
我尝试了这个:
public void killFood ()
{
getWorld().removeObject(this); // >>>>>Kill any object that inherate from Food and operate this method.
}
但它并没有杀死食物固有的类中的任何类型的对象......为什么?
我将它(在 Food 类中)包装为:
public void act()
{
if (canMove())
move();
else
killFood();
}
public boolean canMove()
{
World myWorld = getWorld();
int x = getX();
int y = getY();
y--;
// test for outside border
if (x >= myWorld.getWidth() || y >= myWorld.getHeight())
return false;
else if (x < 0 || y < 0) // if out of the 1st quarter
return false;
return true; // if inside 1st quarter & borders it can be move.
}
但该对象并没有消失...为什么?
谢谢 !!
=================================================== ========================================= 编辑:canMove方法&蘑菇类
public boolean canMove()
{
World myWorld = getWorld();
int x = getX();
int y = getY();
// test for outside border
if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) {
return false;
}
else if (x < 0 || y < 0) {
return false;
}
return true;
}
public class Mushroom extends Food
{
private final int NEED_TOGO_LEFT = 3;
private int mushroomGotDown=0; // counter for regular +1 down steps
public void move()
{
mushroomGotDown++;
// if mushroom didn't got down 2 times, go down one time.
if (mushroomGotDown != NEED_TOGO_LEFT)
setLocation(getX() , getY() + 1);
else // mushroom got down twise, third will be 1 step left.
{
setLocation(getX() - 1 , getY());
mushroomGotDown=0;
}
}
} // end of class Mushroom
Lets say a graphic World,lets say an API of a World, and Actor, and I built an object from new class name Food that inherent from Actor but in certain situations I need the object to be disappeared from my world.
what should be a good way to do so ?
I tried this:
public void killFood ()
{
getWorld().removeObject(this); // >>>>>Kill any object that inherate from Food and operate this method.
}
But it didn't killed any kind of object from Class that inherent from Food... why ?
I wrapped it (in the Food class) with:
public void act()
{
if (canMove())
move();
else
killFood();
}
public boolean canMove()
{
World myWorld = getWorld();
int x = getX();
int y = getY();
y--;
// test for outside border
if (x >= myWorld.getWidth() || y >= myWorld.getHeight())
return false;
else if (x < 0 || y < 0) // if out of the 1st quarter
return false;
return true; // if inside 1st quarter & borders it can be move.
}
But the object did not disappeared... why ?
Thanks !!
==========================================================================================
EDIT: canMove method & Mushroom Class
public boolean canMove()
{
World myWorld = getWorld();
int x = getX();
int y = getY();
// test for outside border
if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) {
return false;
}
else if (x < 0 || y < 0) {
return false;
}
return true;
}
public class Mushroom extends Food
{
private final int NEED_TOGO_LEFT = 3;
private int mushroomGotDown=0; // counter for regular +1 down steps
public void move()
{
mushroomGotDown++;
// if mushroom didn't got down 2 times, go down one time.
if (mushroomGotDown != NEED_TOGO_LEFT)
setLocation(getX() , getY() + 1);
else // mushroom got down twise, third will be 1 step left.
{
setLocation(getX() - 1 , getY());
mushroomGotDown=0;
}
}
} // end of class Mushroom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您的代码如下所示:
从表面上看,这应该可行。
芝士汉堡没有被删除的可能原因包括:
act()
,getWorld()
返回一个不同的世界您所期望的,World.removeObject(...)
无法正常工作,act
或canMove
或killFood
和重写方法做了错误的事情。所有这些场景都可以概括为“错误在其他地方”。
假设您已经检查了代码但找不到问题,下一步应该是使用调试器运行它,并单步执行无法正常工作的代码,看看到底发生了什么。
I assume that your code looks like this:
On the face of it, that should work.
Possible reasons why a cheezeburger doesn't get removed include:
act()
on the cheezeburger,getWorld()
returns a different world to the one you are expecting,World.removeObject(...)
is not working properly,act
orcanMove
orkillFood
and the override method(s) do the wrong thing.All of these scenarios can be summarized as "the bug is somewhere else".
Assuming that you've inspected your code and cannot find the problem, the next step should be to run it using a debugger, and single step it through the code that is not working to see what is really happening.
看起来
getWorld().removeObject(this)
只会从您的世界中删除特定的实例。它不会从您的世界中删除特定类的所有实例。您需要对要删除的类的每个特定实例调用 getWorld().removeObject。
因此,要了解它是如何工作的,请尝试这样的操作:
无论如何,我认为从您的问题来看,您将类的实例与类本身混淆了。
当然,这是假设
removeObject
有效。It looks the
getWorld().removeObject(this)
will only remove a specific instance from your world. It won't remove all instances of a specific class from your world.You need to call getWorld().removeObject on each specific instance of a class that you want to remove.
So to see how this works try something like this:
Anyways, I think from your questions, that you are confusing instances of a class with the class itself.
This is of course assuming that
removeObject
works.