我正在用java編寫一個shell實現,它支持通過向Map<String, Command>
添加一個Command
對象並編寫與此命令關聯的類來添加新命令。java:dynamic class cast
commandList.put("sort", new sort()); // sort extends Command
每個命令具有多個OptionsDefinition<T>
S的選項名稱到使用Consumer<T>
物體的方法相關聯的參考(T是類的命令的:排序例如)。
我要讓引用的方法爲了能夠調用兩次相同的命令在我的殼沒有混合在同一個命令的屬性值,所以我需要傳遞sort
型this
的非靜態Consumer<sort>
在我的sort命令的例子中。
我想知道我怎麼能改造一下我:
Command sort = commandList.getCommand("sort");
到:
sort sort = commandList.getCommand("sort");
能夠使用
option.getCallback().process(sort); // sort is the reference to this
是否有可能做所以?
如果你只有一個'sort'的實現,我只需要使用那一個實現,不需要查看它。如果你有多個實現,我會使用一個強制轉換。 –