[英]Error while using the closure in groovy
我也是新手。 我有简单的代码来学习groovy闭包。 我的代码是
class function1{
static void main(def args){
square = {it * it}
[1,2,3].each(square);
}
}
因此,程序的输出应类似于1,4,9
。 但是我得到了错误
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/home/xxx/GroovyTest/example2.groovy: 4: Apparent variable 'square' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'square' but left out brackets in a place not allowed by the grammar.
@ line 4, column 3.
square = {it * it}
^
/home/xxx/GroovyTest/example2.groovy: 5: Apparent variable 'square' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'square' but left out brackets in a place not allowed by the grammar.
@ line 5, column 16.
[1,2,3].each(square)
^
2 errors
我没有得到如何在静态范围内找到可变平方。
谢谢
您忘记了def
关键字:
static void main(def args){
def square = {it * it}
[1,2,3].each(square);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.