假設我有一個簡單的程序,它採用自變量輸入以下列形式
DO1 inputLocation outputLocation
DO2 inputLocation outputLocation
DO3 [30或60或90] inputLocation outputLocation一個
DO4 [PD或C] inputLocation outputLocation
DO5 [GHI] inputLocation outputLocation如何處理命令行參數中的Java
我也有5個功能與程序相同的名稱,我需要調用。到目前爲止,我還以爲做這種方式(在「半僞」)
static void main(String[] args)
{
if (args.length == 3)
processTriple(args);
if (args.length == 4)
processQuadruple(args);
throw new UnsupportedOperationException("dasdhklasdha");
}
在處理功能看起來像這樣
processDouble(String args[])
{
String operation = "args[0]";
Location input = getInput(args[1]);
Location output = getInput(args[2]);
if (operation.equals("do1"))
do1(input,output);
if (operation.equals("do2"))
do2(input,output);
... etc
}
我做它似乎不是很可擴展的方式。如果一個函數的參數改變了,或者添加了新的函數,看起來這樣做會很痛苦。 什麼是「最好」的方式去這樣的事情
可能重複(http://stackoverflow.com/問題/ 1200054/JAVA庫換解析的命令行參數) –