我有如下示例代码
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 要求括号中的负数。