Given the folowing, in pseudocode
abstract public class Bug {
private static int BREEDTIME;
public void breed() {
if (this.age % this.BREEDTIME) {
world.setAt(new this.class(newX, newY, this.world);
}
}
}
public class Ant extends Bug {
private static int BREEDTIME = 3;
}
public class Doodlebug extends Bug {
private static int BREEDTIME = 8;
}
Is there a way to define the breed() method such that it depends on the BREEDTIME of whatever subclass is calling it? Each subclass of bug is guaranteed to have BREEDTIME initialized.
Also, Ants should breed other Ants, so the constructor called inside breed() has to be the constructor of the type of the subclass that is calling breed().
Am I barking up the wrong tree here?
I would use an abstract method to do this. Define getBreedTime() as abstract in your superclass and then implemented it in each subclass to return the appropriate constant.
You can’t really do what you want using static fields like this.