如何使用Java中的方法删除对象?

如何使用Java中的方法删除对象?,java,Java,我试图编写一个程序,使一只龙虾可以被鹈鹕吃掉,但鹈鹕必须在吃了龙虾后消失。什么代码能把鹈鹕从世界上除名 当前(完整)代码为: 导入绿足。*; /** *在这里写下鹈鹕类的描述。 * *@author(你的名字) *@version(版本号或日期) */ 公共级鹈鹕 { /** *做鹈鹕想做的任何事情。这个方法在任何时候都被调用 *在环境中按下“Act”或“Run”按钮。 */ 私有布尔值lobstereated=false; 公共无效法 { 随机转向(); 转弯(); lookForLobst

我试图编写一个程序,使一只龙虾可以被鹈鹕吃掉,但鹈鹕必须在吃了龙虾后消失。什么代码能把鹈鹕从世界上除名

当前(完整)代码为:

导入绿足。*;
/**
*在这里写下鹈鹕类的描述。
* 
*@author(你的名字)
*@version(版本号或日期)
*/
公共级鹈鹕
{
/**
*做鹈鹕想做的任何事情。这个方法在任何时候都被调用
*在环境中按下“Act”或“Run”按钮。
*/
私有布尔值lobstereated=false;
公共无效法
{
随机转向();
转弯();
lookForLobster();
move();
}
公众假期(星期日)
{
if(Greenfoot.getRandomNumber(100)getWorld().getWidth()-20)
返回true;
如果(getY()<20 | | getY()>getWorld().getHeight()-20)
返回true;
其他的
返回false;
}
/**
*如果我们可以在当前位置看到类“clss”的对象,则返回true。
*如果此处没有此类对象,则为False。
*/
公共布尔canSee(类clss)
{
Actor=getOneObjectAtOffset(0,0,clss);
返回actor!=null;
}
/**
*尝试吃掉“clss”类的对象。只有在
*就是我们当前所在的对象。否则此方法会
*没什么。
*/
公共场所(clss类)
{
Actor=getOneObjectAtOffset(0,0,clss);
if(actor!=null){
getWorld().removeObject(actor);
}
}
}

您需要调用
getWorld().removeObject(this)鹈鹕lookForLobster方法的内部。或者您可以重写Pelican的
eat()
方法并在那里执行,但只能在调用
super.eat()之后执行第一个

请发布代码。我不知道你在问什么,也不知道你可能有什么问题。请查看以及部分,以了解有关如何改进您的问题并增加获得体面帮助的机会的更多信息。您所说的“消失”是什么意思?你的意思是说所有对该对象的引用都将丢失吗?告诉你的鹈鹕使用abracardabra进行魔法咒语,这样龙虾就可以消失了D:P我的意思是,我现在有一个方法,它包括:public void lookForLobster(){eat(Lobster.class);}
import greenfoot.*;

/**
 * Write a description of class Pelican here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Pelican extends Animal
{
    /**
     * Act - do whatever the Pelican wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private boolean lobsterEaten=false;
    public void act() 
    {
        randomTurn();
        turnAtEdge();
        lookForLobster();
        move();
    }
    public void randomTurn()
    {
        if(Greenfoot.getRandomNumber(100)<10)
        {
            turn(Greenfoot.getRandomNumber(91)-45);
        }
    }
    public void turnAtEdge()
    {
        if(atWorldEdge())
        {
            turn(17);
        }
    }
    public void lookForLobster()
    {
        eat(Lobster.class);
    }
}
import greenfoot.*;

import java.util.List;
import java.util.ArrayList;

/**
 * Animal. This is the base class for all animals. In addition to the standard Actor
 * methods, it provides the ability to move and turn.
 * 
 * @author Michael Kolling
 * @version 1.0
 */
public class Animal extends Actor
{
    private static final double WALKING_SPEED = 5.0;

    /**
     * Constructor for Animal - nothing to do.
     */
    public Animal()
    {
    }

    /**
     * Act - empty method. Animals have no default action.
     */
    public void act()
    {
    }


    /**
     * Turn 'angle' degrees towards the right (clockwise).
     */
    public void turn(int angle)
    {
        setRotation(getRotation() + angle);
    }


    /**
     * Move forward in the current direction.
     */
    public void move()
    {
        double angle = Math.toRadians( getRotation() );
        int x = (int) Math.round(getX() + Math.cos(angle) * WALKING_SPEED);
        int y = (int) Math.round(getY() + Math.sin(angle) * WALKING_SPEED);

        setLocation(x, y);
    }


    /**
     * Test if we are close to one of the edges of the world. Return true is we are.
     */
    public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
    }


    /**
     * Return true if we can see an object of class 'clss' right where we are. 
     * False if there is no such object here.
     */
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }


    /**
     * Try to eat an object of class 'clss'. This is only successful if there
     * is such an object where we currently are. Otherwise this method does
     * nothing.
     */
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
}