shell java:动态类转换
我正在用java编写一个shell实现,它支持通过将Command
对象添加到Map<String, Command>
并编写与此命令关联的类来添加新命令
commandList.put("sort", new sort()); // sort extends Command
每个命令都有多个OptionsDefinition<T>
,它们使用Consumer<T>
对象将选项名与方法引用相关联(T是命令的类:例如sort)
我想使引用的方法非静态,以便能够在shell中调用同一命令两次,而不混合同一命令属性的值,因此在我的sort命令示例中,我需要将类型为sort
的this
传递给Consumer<sort>
我想知道如何改变我所拥有的:
Command sort = commandList.getCommand("sort");
为此:
sort sort = commandList.getCommand("sort");
能够使用
option.getCallback().process(sort); // sort is the reference to this
有可能吗
# 1 楼答案
可以使用隐式强制转换实现getCommand
然后你就可以写了
注:
取消显示警告