[英]groovy.lang.MissingMethodException while trying to pass a closure as a parameter
I am new to groovy and am trying to pass a closure as a parameter to a method , below is my code , I am using Groovy 2.4 我是groovy的新手,正在尝试将闭包作为参数传递给方法,以下是我的代码,我正在使用Groovy 2.4
class Test
{
def testMethod()
{
def cl = {a,b -> println "a = "+${a}+" b = "+${b}}
testClosure(cl);
}
def testClosure(closure)
{
closure(5,2);
}
}
I am getting the below exception when i am trying to execute it , 我在尝试执行以下异常时,
Caught: groovy.lang.MissingMethodException: No signature of method:
com.gr.practice.Test.$() is applicable for argument types:
(com.gr.practice.Test$_testMethod_closure1$_closure2) values:
[com.gr.practice.Test$_testMethod_closure1$_closure2@3e92efc3]
Possible solutions:
is(java.lang.Object),
any(),
any(groovy.lang.Closure),
use([Ljava.lang.Object;),
wait(),
dump()
groovy.lang.MissingMethodException: No signature of method:
com.gr.practice.Test.$() is applicable for argument types:
(com.gr.practice.Test$_testMethod_closure1$_closure2) values:
[com.gr.practice.Test$_testMethod_closure1$_closure2@3e92efc3]
Possible solutions:
is(java.lang.Object),
any(),
any(groovy.lang.Closure),
use([Ljava.lang.Object;),
wait(),
dump()
at com.gr.practice.Test$_testMethod_closure1.doCall(Test.groovy:10)
at com.gr.practice.Test.testClosure(Test.groovy:16)
at com.gr.practice.Test$testClosure$0.callCurrent(Unknown Source)
at com.gr.practice.Test.testMethod(Test.groovy:11)
at com.gr.practice.Test$testMethod.call(Unknown Source)
at com.gr.practice.main.run(main.groovy:7)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Could anyone please help ? 有人可以帮忙吗?
Your problem is println "a = "+${a}+" b = "+${b}
. 您的问题是
println "a = "+${a}+" b = "+${b}
。 You probably want this: 您可能想要这样:
println "a = ${a} b = ${b}"
Or: 要么:
println "a = " + a + " b = " + b
(the former is a better idea) (前者是一个更好的主意)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.