0

私もグルーヴィーな新人です。グルーヴィーなクロージャーを学ぶための簡単なコードがあります。私のコードは

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

静的スコープで変数 square がどのように見つかったかわかりません。

ありがとうございました

4

1 に答える 1

4

defキーワードを忘れました:

 static void main(def args){
     def square = {it * it}
     [1,2,3].each(square);
 }
于 2013-09-09T07:13:43.280 に答える