捕获:groovy.lang.MissingMethodException:没有方法的签名
简单代码:捕获:groovy.lang.MissingMethodException:没有方法的签名,groovy,Groovy,简单代码:ClosuresSyntax.groovy { -> item++ } { item -> item++ } 它会导致一个异常: Caught: groovy.lang.MissingMethodException: No signature of method: com.lucaslee.groovy.syntax.ClosuresSyntax$_run_closure1.call() is applicable for argument t
ClosuresSyntax.groovy
{ -> item++ }
{ item -> item++ }
它会导致一个异常:
Caught: groovy.lang.MissingMethodException: No signature of method:
com.lucaslee.groovy.syntax.ClosuresSyntax$_run_closure1.call() is applicable for argument types:
(com.lucaslee.groovy.syntax.ClosuresSyntax$_run_closure2) values:
[com.lucaslee.groovy.syntax.ClosuresSyntax$_run_closure2@1534f01b]
您的代码与相同(请注意括号):
这两个闭包的定义完全正确。问题是,实际上第一个闭包是作为参数传递的第二个闭包运行的。这是完全相同的:
{ it -> it() } { println 1 }
由于无法对闭包
对象调用++
,因此会引发MissingMethodException
。这将正常工作,例如:
{ item -> item()++ }{ 1 }
闭包{1}
作为参数传递,调用()
,结果递增++
要验证闭包定义是否正确,请运行:
def a = { -> item++ }
def b = { item -> item++ }
我的答案解决问题了吗?如果是,请接受。
def a = { -> item++ }
def b = { item -> item++ }