我有幾個擴展父類的子類,被迫擁有一個統一的構造函數。我有一個隊列,它保存了這些類的列表,它們必須擴展MergeHeuristic。我目前擁有的代碼如下所示:任何方式來簡化這與動態類實例化的形式?
Class<? extends MergeHeuristic> heuristicRequest = _heuristicQueue.pop();
MergeHeuristic heuristic = null;
if(heuristicRequest == AdjacentMACs.class)
heuristic = new AdjacentMACs(_parent);
if(heuristicRequest == SimilarInterfaceNames.class)
heuristic = new SimilarInterfaceNames(_parent);
if(heuristicRequest == SameMAC.class)
heuristic = new SameMAC(_parent);
有什麼方法來簡化動態實例化類,沿着線的東西:
heuristic = new heuristicRequest.somethingSpecial();
這將壓平的該塊如果聲明。
@jlordo不,他們不會,'heuristicRequest'是'Class'對象。 – Brian
sry,我的眼睛比我的大腦快:D – jlordo