Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在第一个类的arraylist中的对象上运行另一个类中的方法_Java_Arraylist - Fatal编程技术网

Java 在第一个类的arraylist中的对象上运行另一个类中的方法

Java 在第一个类的arraylist中的对象上运行另一个类中的方法,java,arraylist,Java,Arraylist,我有下面的代码,这是失败的测试线束,我不知道为什么。在我的Hive类中,我有一个名为cells的蜜蜂数组列表和另一天名为anotherDay的方法,我想在数组列表中循环,并在其上调用每只蜜蜂anotherDay方法。守则: ArrayList<Bee> cells = new ArrayList<Bee>(); public void anotherDay(){ for(int i = 0;i<cells.size(); i++){ getBee(

我有下面的代码,这是失败的测试线束,我不知道为什么。在我的
Hive
类中,我有一个名为cells的蜜蜂数组列表和另一天名为
anotherDay
的方法,我想在数组列表中循环,并在其上调用每只蜜蜂
anotherDay
方法。守则:

ArrayList<Bee> cells = new ArrayList<Bee>();

public void anotherDay(){
    for(int i = 0;i<cells.size(); i++){
    getBee(i).anotherDay();
    }
}

基本上,当蜂巢中的
另一天
方法运行时,我希望它在蜂王类中对蜂王运行
另一天
方法,这将使其年龄增加1岁,每3天产卵一次,并将其添加到蜂巢中的阵列列表中,然后运行eat方法。我的测试工具正在蜂箱中运行另一天的
方法,但失败了,因为我的蜂王活得太长,产卵量不够。我哪里出了问题?

你的预期/实际结果是什么?蜂箱中蜂蜜的数量是多少?蜂箱中蜂蜜的初始数量是10,但测试工具会移除所有蜂蜜,然后运行h.anotherDay()3次,这会将皇后的健康降低到0并杀死她,但事实并非如此。我的测试工具是说我的蜂王活得太长,产卵太多。我看不出代码本身有什么问题。但是我可以问一下你的蜂箱是如何生产蜂蜜的(一开始有多少蜜蜂)?由于女王已经11岁了,她在第一天就下了一个蛋。有没有可能是一只早熟的蜜蜂导致了女王活得太久?
public class Queen extends Bee{

    Hive hive = null;   

    public Queen(){
    }

    public Queen(Hive hive){
        this.hive = hive;
        this.type = 1;
        this.age = 11;
        this.health = 3;
    }

    public Bee anotherDay(){
        eat();
        age++;
        if(age%3 == 0){
            hive.addBee(new Egg());
        }
        return this;
    }

    public boolean eat(){
        if(hive.Honey > 2){
            hive.takeHoney(2);
            if(health == 3){
            }else{
                health= health++;
            }
            return true;
        }else{
            health = health -1;
            return false;
        }   
    }

    public int getType(){
        return type;
    }
}