![](/img/trans.png)
[英]Groovy : groovy.lang.MissingMethodException: No signature of method
[英]Caught: groovy.lang.MissingMethodException: No signature of method
簡單代碼: 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]
您的代碼與(請注意括號)相同:
{ -> item++}({ item -> item++})
兩個閉包的定義都是完全正確的。 問題在於實際上第一個閉包是通過第二個閉包作為參數來運行的。 這是完全一樣的:
{ it -> it() } { println 1 }
由於無法在Closure
對象上調用++
,因此會拋出MissingMethodException
。 這將正常工作,例如:
{ item -> item()++ }{ 1 }
閉包{1}
作為參數傳遞,被調用()
,結果遞增++
。
要驗證閉包定義正確,請運行:
def a = { -> item++ }
def b = { item -> item++ }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.