在 Java 中删除对象时出现问题

发布于 2024-11-06 11:13:05 字数 2293 浏览 0 评论 0原文

让我们说一个图形世界,让我们说一个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 技术交流群。

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

发布评论

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

评论(2

神经大条 2024-11-13 11:13:05

我假设您的代码如下所示:

public abstract class Food {
    ...
    public void killFood () {
        getWorld().removeObject(this);
    }

    public void act() {
        if (canMove()) {
            move();
        } else { 
            killFood();
        }
    }
}

public class Cheezeburger extends Food {
    ...
}

从表面上看,这应该可行。

芝士汉堡没有被删除的可能原因包括:

  • 您的代码实际上并未对芝士汉堡调用 act()
  • getWorld() 返回一个不同的世界您所期望的,
  • World.removeObject(...) 无法正常工作,
  • Cheezeburger 类(或超类)已覆盖 actcanMovekillFood 和重写方法做了错误的事情。

所有这些场景都可以概括为“错误在其他地方”。


假设您已经检查了代码但找不到问题,下一步应该是使用调试器运行它,并单步执行无法正常工作的代码,看看到底发生了什么。

I assume that your code looks like this:

public abstract class Food {
    ...
    public void killFood () {
        getWorld().removeObject(this);
    }

    public void act() {
        if (canMove()) {
            move();
        } else { 
            killFood();
        }
    }
}

public class Cheezeburger extends Food {
    ...
}

On the face of it, that should work.

Possible reasons why a cheezeburger doesn't get removed include:

  • your code isn't actually calling act() on the cheezeburger,
  • getWorld() returns a different world to the one you are expecting,
  • World.removeObject(...) is not working properly,
  • the Cheezeburger class (or a superclass) has overridden act or canMove or killFood 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.

梦萦几度 2024-11-13 11:13:05

看起来 getWorld().removeObject(this) 只会从您的世界中删除特定的实例。它不会从您的世界中删除特定类的所有实例。

您需要对要删除的类的每个特定实例调用 getWorld().removeObject。

因此,要了解它是如何工作的,请尝试这样的操作:

Food foo1 = new Food();
Food foo2 = new Food();
Food foo3 = new Food();

World world = new World();
world.add(foo1, ...);  //Be sure to place each object in a distinct position so you can see each one.
world.add(foo2, ...);  
world.add(foo3, ...);

//Now delete one. 
foo1.killFood(); 

//one of th ethree should go away. 

无论如何,我认为从您的问题来看,您将类的实例与类本身混淆了。

当然,这是假设 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:

Food foo1 = new Food();
Food foo2 = new Food();
Food foo3 = new Food();

World world = new World();
world.add(foo1, ...);  //Be sure to place each object in a distinct position so you can see each one.
world.add(foo2, ...);  
world.add(foo3, ...);

//Now delete one. 
foo1.killFood(); 

//one of th ethree should go away. 

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.

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