在不同的包java

时间:2019-01-02 22:08:04

标签: java

我想知道是否有办法从不同包中的8个不同类中获取多个变量(其中12个):
(人类,老人(8之3),人类,成人(8之2)和人类儿童(8之3))。
这是其中一个类的示例:

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
    }
}

因此,对于像上一个类一样的8个类中的每一个,我都可以调用BaseHuman扩展的所有12个getter方法,因此最终将调用96个getter方法。抱歉,我刚接触Java。

我该怎么做?

-谢谢!

2 个答案:

答案 0 :(得分:1)

继承的优点之一是,您可以在从其继承的每个Class上调用BaseHuman的方法(例如Brother)。因此,您可以像这样列出BaseHuman:

ArrayList<BaseHuman> bhList = new ArrayList<BaseHuman>();

,然后将Brothers和其他所有内容添加到列表中。

Brother broA = new Brother();
Sister sisA = new Sister();
bhList.add(broA);
bhList.add(sisA);

然后您可以像这样在列表中存储的每个对象中调用吸气剂:

for(BaseHuman bh : bhList){
    System.out.println(bh.get());
}

要求是,您必须覆盖超类BaseHuman中的getter方法。

如果您想了解更多信息,可以研究“多态”。

答案 1 :(得分:0)

我不确定自己是否理解问题的目的,因此,如果我误解了,请告诉我。

如果您真的想在每个班级中调用所有吸气剂,则可以使用@ M.Dan提供的解决方案。但是,对我来说,这听起来像是您正在尝试创建一个继承自library(shiny) library(readxl) library(tidyverse) library(xgboost) library(caret) library(iml) #### UI ui <- fluidPage( sidebarLayout( sidebarPanel( fileInput("file1", "Choose CSV File", accept = c( "text/csv", "text/comma-separated-values,text/plain", ".csv") ), tags$hr(), checkboxInput("header", "Header", TRUE) ), mainPanel( #tableOutput("contents"), plotOutput("plot1", click = "plot_brush") ) ) ) server <- function(input, output) { # create mydata as a reactiveVal so that it can be edited everywhere mydata = reactiveVal() # reactive block is changed with an observe that allows mydata to be updated # on change of data observe({ req(input$file1, input$header, file.exists(input$file1$datapath)) data = read.csv(input$file1$datapath, header = input$header) mydata(data) }) output$contents <- renderTable({ req(mydata()) #mydata() }) ### test xgb_trcontrol = trainControl( method = "cv", number = 5, allowParallel = TRUE, verboseIter = FALSE, returnData = FALSE ) #I am specifing the same parameters with the same values as I did for Python above. The hyperparameters to optimize are found in the website. xgbGrid <- expand.grid(nrounds = c(10,14), # this is n_estimators in the python code above max_depth = c(10, 15, 20, 25), colsample_bytree = seq(0.5, 0.9, length.out = 5), ## The values below are default values in the sklearn-api. eta = 0.1, gamma=0, min_child_weight = 1, subsample = 1 ) # note that obsB reactive variable is gone. if you don't use a # reactive variable, the code block will not be executed. # unlike observe blocks, reactive blocks are lazy and should # not be relied on for their side effects observe({ # this if ensures you don't run this block before mydata isn't a data frame # also prevents it running after it updates mydata. otherwise this will # be executed twice. its an invisible problem that'll make it run half # as fast unless you debug. if ('data.frame' %in% class(mydata()) & !'predicted' %in% names(mydata())){ set.seed(0) xgb_model = train( mydata()["LotArea"], as.vector(t(mydata()["SalePrice"])), trControl = xgb_trcontrol, tuneGrid = xgbGrid, method = "xgbTree" ) predicted = predict(xgb_model, mydata()["LotArea"]) data = mydata() data["predicted"] = predicted mydata(data) } }) output$plot1 <- renderPlot({ data = mydata() # this is here to prevent premature triggering of this ggplot. # otherwise you'll get the "object not found" error if('predicted' %in% names(data)){ ggplot(mydata(), aes(x=predicted, y=SalePrice)) + geom_point() } }) } shinyApp(ui, server) 的类的实例,因此可以更改其属性。例如,也许BaseHuman的{​​{1}}值较低,但在其他方面相同。

您可以通过调用OldHuman来简单地使用HEALTH类构造函数,而不必再次调用所有的getter来设置字段:

BaseHuman

这样,您可以节省自己重写96次所有字段的权限,并且仅更改所需的内容。而且,您的super()的行为将完全相同(getter,setter等)

如果您仍然真的需要调用所有的getter,则至少可以在class OldHuman extends BaseHuman { public OldHuman() { super(); //invokes parent constructor which you supplied default values this.HEALTH = 75; //change HEALTH to desired amount instead } } 中编写一个方法,该方法返回某种Collection中的所有字段,然后让您的类继承该方法。 / p>