Pergunta

I have a class Worker which I want to access methods from hive, garden and all the subclasses of the superclass flower. A worker is in an arraylist in hive which is in a garden and the garden also has an arraylist of flowers. I have accessed the methods from hive using the constructor:

public worker(Hive hive){
    this.hive= hive
}

And I want to access the method findFlower() from the garden to get a flower and extractPollen() (which every type of flower inherits from the super class flower) from the flowers I get to get pollen from them. Do I need to make more constructors for garden and for each type of flower or will 2 constructors, 1 for the garden and 1 for the super constructor flower work?

My code so far:

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;
    }
}
Foi útil?

Solução

May i suggest this...

A worker can't "work" without both a Hive or Garden. Supply both. Also, flower doesnt need to be a member variable. Its local to anotherDay(). Also, anotherDay() need not return Bee. The caller has reference to your object anyway.

public class Worker extends Bee {
    private Hive hive;
    private Garden garden;

    public Worker(Hive hive, Garden garden){
        super(hive);
        this.hive=hive;
        this.garden = garden;
        this.type = 2;
        this.age=11;
        this.health=3;
    }

    public void anotherDay(){
        Flower flower = garden.findFlower();
        flower.extractPollen(/* some value */);
        eat();
        age++;
    }
}

Another approach is that Hive and Garden are not members at all, but are passed in for each methed call. eg anotherDay(Garden garden); the benefits of this are that your worker can roam gardens. The drawback is that the calling code has to manage the Garden object. These are the trade offs you make as you do OO design :)

Outras dicas

You could try;

public class Garden {

    ArrayList<Flower> flowerbed = new ArrayList<Flower>();
    private static Garden instance;

    public Flower findFlower(){
        //code which returns a random flower from my arraylist
   }

    public static Garden getInstance() {
         if (instance == null) {
              instance = new Garden();
         }
         return instance;
     }
}

This will return an instance of Garden and allow you to access findFlower with

Garden.getInstance().findFlower();

When you call the getInstance() for the first time it will create a new Garden

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top