亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我可以像參數一樣引用子類嗎?

我可以像參數一樣引用子類嗎?

繁星coding 2023-03-31 09:56:59
我想在我的抽象類中引用任何潛在的子類,如參數,以創建通用函數,例如在不重載的情況下創建子類的新實例。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 回答

?
MYYA

TA貢獻1868條經驗 獲得超4個贊

我想在我的抽象類中引用任何潛在的子類,如參數,以創建通用函數,例如在不重載的情況下創建子類的新實例。


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()

}


有什么辦法可以做這樣的事情嗎?


查看完整回答
反對 回復 2023-03-31
  • 1 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號