我正在构建一个管理控制器,其工作方式类似于 Flex 4.5 中的终端模拟器。
服务器端是Tomcat服务器上的Red5,使用Java编程语言。
当用户在其文本输入中输入命令时,该命令会发送到 red5,在 red5 中,我检查该命令是否存在,如果命令或参数不匹配,则返回正确的输出或错误。
所以现在我用if (command.equals("..") {} else if (command.equals(...
有没有办法存储函数名称或对每个命令中应执行的函数的引用并执行它?
example:
// creating the hasmap
HashMap<String,Object> myfunc = new HashMap<String,Object>();
// adding function reference
myfunc.put("help",executeHelp);
or....
myfunc.put("help", "executeHelp"); // writing the name of the function
and then
void receiveCommand(String command, Object params[]( {
myfunc.get(command).<somehow execute the referrened function or string name ? >
}
有任何想法吗?
谢谢你!
您可以使用反射,但我建议使用更简单的方法。
您可以使用抽象方法执行创建抽象类或接口。例子:
interface Command {
void execute(Object params[]);
}
class Help implements Command {
void execute(Object params[]) {
// do the stuff
}
}
现在你的哈希图可以是:
// creating the hasmap
HashMap<String,Command> myfunc = new HashMap<String,Command>();
// adding function reference
myfunc.put("help", new Help());
进而:
void receiveCommand(String command, Object params[]) {
myfunc.get(command).execute(params);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)