[英]get same 12 variables from 8 classes in different packages java
I am wondering if there is a way to get multiple variables (12 of them) from 8 different classes in different packages: 我想知道是否有办法从不同包中的8个不同类中获取多个变量(其中12个):
(Humans.Old (3 of 8), Humans.Adult (2 of 8), and Humans.Child (3 of 8)). (人类老(3之8),人类成人(2之8)和人类儿童(8之3))。
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. 因此,对于最上面的8个类中的每一个,我都可以调用BaseHuman扩展的所有12个getter方法,因此最终将调用96个getter方法。 Sorry, I'm fairly new to Java.
抱歉,我刚接触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). 继承的优点之一是,您可以在从其继承的每个类(例如Brother)上调用BaseHuman的方法。 So you can make a list of BaseHuman like this:
因此,您可以像这样列出BaseHuman:
ArrayList<BaseHuman> bhList = new ArrayList<BaseHuman>();
and then add Brothers and every thing else to the list. 然后将Brothers和其他所有内容添加到列表中。
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: 那么您可以像这样在列表中存储的每个对象中调用getter:
for(BaseHuman bh : bhList){
System.out.println(bh.get());
}
The requirement is, that you overwrite the getter Method from the Superclass BaseHuman. 要求是您覆盖超类BaseHuman的getter方法。
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. 如果您真的只想调用每个类中的所有getter,则可以使用@ M.Dan提供的解决方案。 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. 但是,对我来说,这听起来像是您正在尝试创建一个继承自
BaseHuman
的类的实例,以便可以更改其属性。 For example, maybe OldHuman
has a lower value for HEALTH
but is otherwise the same. 例如,
OldHuman
的HEALTH
值可能较低,但其他方面相同。
Instead of calling all the getters again to set the fields, you can simply use the BaseHuman
class constructor by calling super()
: 您不必通过再次调用所有getter来设置字段,而只需通过调用
super()
即可使用BaseHuman
类构造函数:
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. 这样,您就可以节省自己重写96次所有字段的时间,并且仅更改所需的内容。 And, your
OldHuman
will otherwise behave exactly the same (getters, setters, etc.) 并且,您的
OldHuman
行为将完全相同(getter,setter等)。
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. 如果您仍然真的需要调用所有getter,则至少可以在
BaseHuman
中编写一个方法,该方法返回某种Collection中的所有字段,然后让您的类继承该方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.