我可以像参数一样引用子类吗?

时间:2019-07-17 21:46:55

标签: java subclass abstract

我想在我的抽象类中引用任何可能的子类(例如参数),以创建通用函数,例如,在不重载的情况下创建子类的新实例。

abstract class Fictional
{
  public static ArrayList<SUBCLASS_PARAM> subclassArray = new ArrayList<SUBCLASS_PARAM>();
  int i;

  private Fictional(int i) //All subclasses have to implement this constructor
  {
     this.i = i;
     //body
  }

  public static SUBCLASS_PARAM loadFromFile() //I wouldn't have to override this method
  {
     SUBCLASS_PARAM subclass = new SUBCLASS_PARAM(1); //it's not possible to make new instance of abstract class, but it would be possible with any other subclass
     subclassList.put(subclass);
     return subclass;
  }
}

class Real extends Fictional
{
//nothing here
}

class main
{
  Real r = Real.loadFromFile()
}

有什么办法做这样的事情吗?

1 个答案:

答案 0 :(得分:0)

您可以使用泛型和子句来做到这一点:

library(dplyr)
library(readr)
library(nnet)
library(sjPlot)

"ASA,DSA,Info_Sought
-0.108555801,0.659899854,First Pic
0.671946671,1.481880373,First Pic
2.184170211,-0.801398848,First Pic
-0.547588442,1.116555698,First Pic
-1.27930951,-0.299077419,First Pic
0.037788412,1.527545958,First Pic
-0.74271406,-0.755733264,Multiple Pics
1.20854212,-1.166723523,Multiple Pics
0.769509479,-0.390408588,Multiple Pics
-0.450025633,-1.02972677,Multiple Pics
0.769509479,0.614234269,Multiple Pics
0.281695434,0.705565438,Multiple Pics
-0.352462824,-0.299077419,Expand
0.671946671,1.481880373,Expand
2.184170211,-0.801398848,Expand
-0.547588442,1.116555698,Expand
-0.157337206,1.070890114,Expand
-1.27930951,-0.299077419,Expand" %>% 
  read_csv() -> d

multinomialmodel <- multinom(Info_Sought ~ ASA + DSA, data = d)

p1 <- plot_model(multinomialmodel , 
                 type = "pred", 
                 terms = c("DSA", "ASA[meansd]"))
p1