使用方法更改ArrayList中的对象并返回

时间:2013-11-26 09:28:00

标签: java arraylist polymorphism return

public class Egg extends Bee{
    protected boolean eat() {
        return true;
    }

    protected static Object anotherDay() {

        eat();
        if(age>=3)
        {
            Larvae larvae = new Larvae();
            return this;
        }
        age++;
        return this; 
    }
}


public abstract class Bee {
    protected String type;
    protected int age;
    protected int health = 3;

    protected String getType()
    {
        return type;
    }
    protected int getAge()
    {
        return age;
    }
    protected int getHealth()
    {
        return health;
    }

    protected void setAge(int age)
    {
        this.age = age;
    }

    protected abstract boolean eat();
    protected abstract static Object anotherDay();  //the bees tasks for day (should include eat())

}

我正在尝试更改anotherDay()方法,因此它将返回一个对象,但我无法弄清楚如何执行此操作。 然后该对象将替换arrayList中的当前对象(arrayList中的Egg对象将变为列表中同一点的Larvae对象) 目前,我发现抽象静态对象是返回一个对象但它无法正常工作。

1 个答案:

答案 0 :(得分:0)

我会将anotherDay方法设置为检查年龄的void方法,如果它是正确的值,则删除蛋并在其索引中添加幼虫。