[英]Dynamically instantiating abstract subclass
First I have to say I am real new to Java. 首先,我不得不说我是Java的新手。 So all apologize if my question sound stupid but I haven't been able to find a solution so far... 所以所有人都道歉,如果我的问题听起来很愚蠢,但到目前为止我还没有找到解决办法......
I would like to dynamically instantiate a abstract class subclass. 我想动态实例化一个抽象类子类。
This is the code 这是代码
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
}
}
When doing so the part 这样做的部分
AbstractCommand command = commandClass.getClass().newInstance();
is giving me an error (illegalAccessException and InstantiationException) in my IDE (not at compile time as I haven't tried it yet) 在我的IDE中给我一个错误(illegalAccessException和InstantiationException)(不是在编译时,因为我还没有尝试过)
so I surround it with try/catch like this 所以我用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
}
but then it tells me that the type Class (sent by the newInstance() ) is obvisouly not of type AbstractCommand. 但后来它告诉我类型Class(由newInstance()发送)显然不是AbstractCommand类型。
When trying to cast Class to AbstractCommand doing 当试图将类转换为AbstractCommand时
AbstractCommand command = (AbstractCommand) commandClass.getClass().newInstance();
It tells me that Class cannot be cast to AbstractCommand. 它告诉我Class不能转换为AbstractCommand。
So I was wondering what I am doing wrong ? 所以我想知道我做错了什么?
Thanks again for any help you could provide. 再次感谢您提供的任何帮助。
I think what you want instead of 我想你想要的而不是
commandClass.getClass().newInstance()
is 是
commandClass.newInstance()
commandClass is, itself, a Class. commandClass本身就是一个Class。 Therefore calling getClass() on it will return java.lang.Class, and if you were to instantiate that (you couldn't, but if you could) it wouldn't be assignable to an AbstractCommand. 因此,在它上面调用getClass()将返回java.lang.Class,如果你要实例化它(你不能,但如果可以的话),它将无法分配给AbstractCommand。 But removing the extra getClass(), you have the class of the command, and when you instantiate it, you'll get an AbstractCommand instance. 但是删除额外的getClass(),你就拥有了命令的类,当你实例化它时,你将获得一个AbstractCommand实例。 Everything else seems fine, I think. 我认为其他一切似乎都很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.