2011-09-06 132 views
3

首先,我必須說我是真正的Java新手。所以,所有的道歉,如果我的問題聽起來很愚蠢,但我一直沒能找到解決方案,迄今...動態實例化抽象子類

我想動態實例化一個抽象類的子類。

這是代碼

public class CommandMap implements Handler.Callback 
{ 
    private HashMap <Integer, Class<? extends AbstractCommand> > __commandHashMap; 

    protected CommandMap() 
    { 
     __commandHashMap = new HashMap<Integer, Class<? extends AbstractCommand>>(); 
    } 

    /** 
    * Map an ID to a command 
    * @param what Id used in the Message sent 
    * @param command Command to be executed 
    */ 
    public void mapWhat(Integer what, Class<? extends AbstractCommand> command) 
    { 
     if (!__commandHashMap.containsKey(what)) 
     { 
      __commandHashMap.put(what, command); 
     } 
    } 

    /** 
    * Unmap the id/command pair 
    * @param what Id 
    */ 
    public void unmapWhat(Integer what) 
    { 
     if (__commandHashMap.containsKey(what)) 
     { 
      __commandHashMap.remove(what); 
     } 
    } 

    public boolean handleMessage (Message message) 
    { 
    // call the corresponding command 
     if (__commandHashMap.containsKey(message.what)) 
     { 
      Class<? extends AbstractCommand> commandClass = __commandHashMap.get(message.what); 
      AbstractCommand command = commandClass.getClass().newInstance(); 
     } 
     return true; // for now  
    } 
} 

在這樣做的部分

AbstractCommand command = commandClass.getClass().newInstance(); 

是給我一個錯誤(illegalAccessException和InstantiationException)在我的IDE(而不是在編譯的時候,因爲我還沒有嘗試它呢)

所以我圍繞它與這樣的嘗試/趕上

public boolean handleMessage (Message message) 
{ 
// call the corresponding command 
    if (__commandHashMap.containsKey(message.what)) 
    { 
     Class<? extends AbstractCommand> commandClass = __commandHashMap.get(message.what); 
     try 
     { 
      AbstractCommand command = commandClass.getClass().newInstance(); 
     } 
     catch (IllegalAccessException e) 
     { 
      e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
     } 
     catch (InstantiationException e) 
     { 
      e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
     } 
    } 
    return true; // for now 
} 

但它告訴我類型Class(由newInstance()發送)顯然不是AbstractCommand類型。

當試圖投類到AbstractCommand做

AbstractCommand command = (AbstractCommand) commandClass.getClass().newInstance(); 

它告訴我,類不能被轉換爲AbstractCommand。

所以我想知道我在做什麼錯?

再次感謝您提供的任何幫助。

回答

4

我想你想,而不是

commandClass.getClass().newInstance() 

是什麼

commandClass.newInstance() 

commandClass是,本身就是一個類。因此,在它上調用getClass()將返回java.lang.Class,並且如果要實例化(你不能,但如果可以的話)它將不能分配給AbstractCommand。但是除去額外的getClass(),你有這個命令的類,當你實例化它時,你會得到一個AbstractCommand實例。我想,其他一切似乎都很好。

+0

getClass()方法獲取實際的子類嗎? 正在做commandClass.newInstance()試圖調用抽象類的構造函數而不是子類? THX您的幫助反正,我來試試這個 – nolazybits

+0

'commandClass.newInstance()'將調用哪個類「commandClass」是默認的構造函數。即如果'commandClass = FooCommand.class',那麼'commandClass.newInstance()'與'new FooCommand()'完全相同。合理? –

+0

所以,是的,它不會抱怨了... THX這個......如果你也能回答上述問題的意見;) THX再次大量... – nolazybits