【发布时间】:2013-12-27 08:12:46
【问题描述】:
我有一个 Worker 类,我想从蜂巢、花园和超类花的所有子类中访问方法。一个工人在一个花园里的蜂巢中的一个数组列表中,花园里也有一个花的数组列表。我已经使用构造函数从 hive 访问了方法:
public worker(Hive hive){
this.hive= hive
}
我想从花园中访问方法 findFlower() 来获取一朵花,然后从我得到的花中提取花粉(每种类型的花都继承自超类花)来获取花粉。我是否需要为花园和每种类型的花制作更多的构造函数,还是需要 2 个构造函数,1 个用于花园,1 个用于超级构造函数?
到目前为止我的代码:
public class Worker extends Bee {
Hive hive = null;
public Garden garden;
public Flower flower;
public Worker(Hive hive){
this.hive=hive;
this.type = 2;
this.age=11;
this.health=3;
}
public Bee anotherDay(){
flower= garden.findFlower();
flower.extractPollen(int);
eat();
age++;
}
}
public class Garden{
ArrayList<Flower> flowerbed = new ArrayList<Flower>();
public Flower findFlower(){
//code which returns a random flower from my arraylist
}
}
public class Flower{
public boolean extractPollen(int po){
if(po <= pollen){
pollen= pollen - po;
return true;
}return false;
}
}
【问题讨论】:
标签: java class methods arraylist superclass