简体   繁体   English

groovy.lang.MissingMethodException尝试将闭包作为参数传递时

[英]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.

相关问题 groovy.lang.MissingMethodException 用于 Groovy 中的“收集”关闭 - groovy.lang.MissingMethodException for 'collect' closure in Groovy 尝试使用视图绑定时出现 groovy.lang.MissingMethodException - groovy.lang.MissingMethodException when trying to use viewbinding Groovy:groovy.lang.MissingMethodException:方法未签名 - Groovy : groovy.lang.MissingMethodException: No signature of method soapUI groovy脚本groovy.lang.MissingMethodException - soapUI groovy script groovy.lang.MissingMethodException groovy.lang.MissingMethodException:IssueLinkImpl而不是IssueLink - groovy.lang.MissingMethodException : IssueLinkImpl instead of IssueLink jenkinspipeline groovy.lang.MissingMethodException:没有方法签名 - jenkinspipeline groovy.lang.MissingMethodException: No signature of method 如何处理 groovy.lang.MissingMethodException - How to deal with groovy.lang.MissingMethodException 捕获:groovy.lang.MissingMethodException:没有方法的签名 - Caught: groovy.lang.MissingMethodException: No signature of method groovy.lang.MissingMethodException:没有方法解析的签名 - groovy.lang.MissingMethodException: No signature of method resolution groovy.lang.MissingMethodException:没有方法的签名: - groovy.lang.MissingMethodException: No signature of method:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM