我希望能够得到一个关于为什么调用这个Groovy方法按预期工作的解释:
def f1(int n) {
return n + 1
}
println f1(1) // -> 2
但是,如果参数没有被明确定义(使用“def n”而不是“int n”),则需要更改方法调用:
def f2(def n) {
return n + 1
}
println f2(1) // Exception: Illegal class name
println this.&f2(1) // -> 2
这是什么原因导致这种情况发生?新增更多信息:这是在Windows上,使用Groovy 2.4.5和JVM 1.8.0_51。整个脚本就是一个名为1.groovy的文件中的9行代码,没有其他内容。我正在控制台(cmdr)中使用“groovy 1.groovy”运行此脚本。第8行的错误是:“Caught: java.lang.ClassFormatError: Illegal class name "3$f2" in class file 3$f2 java.lang.ClassFormatError: Illegal class name "3$f2" in class file 3$f2 at 3.run(3.groovy:8)”
def f3(n) {...
是什么意思?此外,这是 Groovy 的哪个版本? - doelleri