以下のようなサンプルコードがあります
import org.codehaus.groovy.control.CompilerConfiguration
abstract class MyClass extends Script {
void testMethod(Integer x) {
println "x = $x"
}
}
public static void main(String[] args) {
compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setScriptBaseClass("MyClass");
GroovyShell shell = new GroovyShell(new Binding(), compilerConfiguration);
shell.evaluate("testMethod 1")
}
このクラスを実行するとx = 1
、に変更すると印刷"testMethod 1"
され"testMethod -1"
、失敗します
Caught: groovy.lang.MissingPropertyException: No such property: testMethod for class: Script1
groovy.lang.MissingPropertyException: No such property: testMethod for class: Script1
at Script1.run(Script1.groovy:1)
at Test.run(Test.groovy:15)
今私はに変更"testMethod -1"
し"testMethod (-1)"
ます。再び機能し、印刷されましたx = -1
私が理解する必要があるのは、Groovy が負の数の括弧を求めている理由です。