2011-09-06 15 views
3

まず、私はJavaにとって本当の新しいことだと言わなければなりません。だから私の質問がばかげていると謝っていますが、これまでのところ解決策を見つけることができませんでした...抽象サブクラスを動的にインスタンス化する

抽象クラスのサブクラスを動的にインスタンス化したいと思います。私が持っていないとして(コンパイル時ではなく、私のIDEに私にエラー(IllegalAccessExceptionが及びないInstantiationExceptionを)与えているので、一部

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

を行う場合

この

はコード

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

です試してみました)

だからこのようなtry/catchで囲みます。

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 
} 

しかし、それは、(newInstance()によって送られた)タイプClassが、抽象的な型AbstractCommandではないことを私に伝えます。

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

をやっAbstractCommandにクラスをキャストしようとするとそれはクラスがAbstractCommandにキャストすることができないことを私に伝えます。

私は何が間違っているのだろうかと思っていましたか?

お手数をおかけしますが、ありがとうございます。

答えて

4

は、私はあなたが

commandClass.getClass().newInstance() 

するのではなく、何をしたいと思います

commandClass.newInstance() 

commandClass自体は、クラスです。したがって、getClass()を呼び出すとjava.lang.Classが返され、インスタンス化する場合(できなかった場合でも可能ですが)、AbstractCommandに代入することはできません。しかし、追加のgetClass()を削除すると、コマンドのクラスが作成され、インスタンス化すると、AbstractCommandインスタンスが取得されます。他のすべてはうまくいくように思えます。

+0

getClass()メソッドが実際のサブクラスクラスを取得していますか? commandClass.newInstance()は、サブクラスの代わりに抽象クラスのコンストラクタを呼び出そうとしていますか? あなたの助けを借りて、とにかく、私はこれを試してみます – nolazybits

+0

'commandClass"クラスのどちらでも 'commandClass.newInstance()'がデフォルトコンストラクタを呼び出します。私。 'commandClass = FooCommand.class'の場合、' commandClass.newInstance() 'は' new FooCommand() 'とまったく同じです。理にかなっている? –

+0

それでは、それはもはや不平を言うことはありません... Thxこれについて...上記の質問に答えてもらえれば、 Thxもう一度... – nolazybits

関連する問題