0

ファイルがあり、その名前がfile.txtだとします。これは、Javaのリフレクションメソッドのスクリプトで構成されています。その一部を次のように仮定します。

new <id> <class> <arg0> <arg1> … creates a new instance of <class> by using 
a constructor that takes the given argument types and stores the result in <id>.
call <id> <method> <arg0> <arg1> …  invokes the specified <method> that 
takes the given arguments on the instance specified by <id> and prints the answer. 
print <id>  prints detailed information about the instance specified by <id>. See 
below for Object details.

ファイルからのスクリプトは、プログラムで文字列として取得されます。リフレクションのために上記で指定した引数にどのように変換しますか。これについては盲目です!Javaが初めてなので、コードのヘルプを含む簡単な説明をいただければ幸いです。

4

1 に答える 1

1

これは、まず解析の問題です。最初に行う必要があるのは、入力を扱いやすいチャンクに分割することです。コンポーネントを区切るために空白を使用しているように見えるので、それはかなり簡単なことです。

1 行に 1 つのコマンドがあるため、最初にコマンドを行に分割し、次に空白に基づいて個別の文字列に分割します。解析は、独自の質問に値する十分に大きなトピックです。

次に、行ごとに移動し、行の最初の単語で if ステートメントを使用して、実行するコマンドを決定し、実行されている内容に基づいて他の単語を解析します。

このようなもの:

public void execute(List<String> lines){
    for(String line : lines){
        // This is a very simple way to perform the splitting. 
        // You may need to write more code based on your needs.
        String[] parts = lines.split(" ");

        if(parts[0].equalsIgnoreCase("new")){
            String id = parts[1];
            String className = parts[2];
            // Etc...
        } else if(parts[0].equalsIgnoreCase("call")){
            String id = parts[1];
            String methodName = parts[2];
            // Etc...
        }
    }
}
于 2013-03-03T19:17:58.657 に答える