尝试将闭包作为参数传递时发生groovy.lang.MissingMethodException
我是groovy新手,正在尝试将闭包作为参数传递给方法,下面是我的代码,我正在使用groovy 2.4尝试将闭包作为参数传递时发生groovy.lang.MissingMethodException,groovy,Groovy,我是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); } }
class Test
{
def testMethod()
{
def cl = {a,b -> println "a = "+${a}+" b = "+${b}}
testClosure(cl);
}
def testClosure(closure)
{
closure(5,2);
}
}
当我试图执行它时,我得到以下异常
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)
有人能帮忙吗?您的问题是
println“a=“+${a}+”b=“+${b}
。你可能想要这个:
println "a = ${a} b = ${b}"
或:
(前者是一个更好的主意)HI的可能重复您可以将其标记为已修复,问题是我的${variable}应该在引号内。@DanielA.Thompson我认为这不是您引用的问题的重复。这是两个独立的问题。感谢您的回复Jeff,虽然在Intellig Idea中做了相同的修复,但在eclipse中使用groovy eclipse插件面临相同的问题,下面是临时编写的代码,
def static main(String[]args){def cl={a,b->println“a=${a}和b=${b}}testClosure(cl)}私有def testClosure(closure){closure(5,2)}
您知道为什么会发生这种情况吗?println“a=${a}b=${b}”
是有效的Groovy代码,无论您使用的是什么IDE。如果IDE出于某种原因对此进行投诉,这就是IDE错误。这是有效的代码。
println "a = " + a + " b = " + b