I am wondering if there is a way to get multiple variables (12 of them) from 8 different classes in different packages:
(Humans.Old (3 of 8), Humans.Adult (2 of 8), and Humans.Child (3 of 8)).
Here is an example of one of the classes:
package Humans.Adult;
import Humans.BaseHuman;
import java.util.ArrayList;
import java.util.List;
public class Brother extends BaseHuman {
public void init() {
List<Integer> startingItemCodes = new ArrayList<Integer>();
startingItemCodes.add(0);
setMAX_SANE(110); // Part of getters and setters in BaseHuman.java
setMAX_WATER(110); // This also
setMAX_HUNGER(110); // This also
setHEALTH(100); // This also
setHUNGER_DEC(10); // This also
setSANE_DEC(10); // This also
setWATER_DEC(10); // This also
setHEALTH_DEC_HUNGER(10); // This also
setHEALTH_DEC_THIRST(10); // This also
setSICK_CHANCE(0.05f); // This also
setNAME("Brother"); // This also
setSTARTING_ITEMS(startingItemCodes); // This also
}
}
So, for each of the 8 classes like the top one, can I call all of the 12 getter methods extended by BaseHuman, so I would end up calling 96 getter methods. Sorry, I'm fairly new to Java.
How would I do this?
--Thanks!
One of the advantages of inheritance is, that you can call the methods of BaseHuman on every Class that is inheriting from it (eg Brother). So you can make a list of BaseHuman like this:
ArrayList<BaseHuman> bhList = new ArrayList<BaseHuman>();
and then add Brothers and every thing else to the list.
Brother broA = new Brother();
Sister sisA = new Sister();
bhList.add(broA);
bhList.add(sisA);
then you could call getters from every Object that is stored in the list like this:
for(BaseHuman bh : bhList){
System.out.println(bh.get());
}
The requirement is, that you overwrite the getter Method from the Superclass BaseHuman.
If you want to know more you can look into "Polymorphism".
I'm not entirely sure I understand the purpose behind the question, so if I misunderstood please let me know.
If you really just want to call all of the getters in every class, you can use @M.Dan's solution as he provided. But, to me it sounds like you are trying to create an instance of a class that inherits from BaseHuman
, so you can change its properties. For example, maybe OldHuman
has a lower value for HEALTH
but is otherwise the same.
Instead of calling all the getters again to set the fields, you can simply use the BaseHuman
class constructor by calling super()
:
class OldHuman extends BaseHuman {
public OldHuman() {
super(); //invokes parent constructor which you supplied default values
this.HEALTH = 75; //change HEALTH to desired amount instead
}
}
This way, you save yourself rewriting all the fields 96 times, and only change what you need. And, your OldHuman
will otherwise behave exactly the same (getters, setters, etc.)
If you still REALLY need to call all of the getters, you could at the very least write a method in BaseHuman
that returns all fields in a Collection of some sort, and let your classes inherit that instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.