当前位置:网站首页>[groovy] closure (closure parameter list rule | default parameter list | do not receive parameters | receive custom parameters)

[groovy] closure (closure parameter list rule | default parameter list | do not receive parameters | receive custom parameters)

2022-07-05 04:40:00 Programmer community

List of articles

  • One 、 Closure parameter list
  • Two 、 Closure parameter list code example

One 、 Closure parameter list


The parameter settings of closures are as follows :

  • Do not receive parameters : If when defining closures , Only written. " -> " Symbol , There is no parameter list defined , Indicates the closure Do not want to receive any parameters , The incoming parameter will report an error ;
  • Default parameter list : If when defining closures , Didn't write " -> " Symbol , also There is no parameter list defined , Closure is OK Receive a default parameter it ;
  • Receive custom parameters : If when defining closures , Yes " -> " Symbol , also Define parameter list , Closure Parameters must be passed in according to the parameter list ;

If only " -> " Symbol , There is no parameter list defined , If you still pass parameters into the closure , An error will be reported at run time , Reference resources 【 Error log 】Groovy Operation error reporting ( Exception in thread “main“ groovy.lang.MissingMethodException: No signature of ) Blog ;

 Insert picture description here

Two 、 Closure parameter list code example


Code example :

import org.codehaus.groovy.ant.Groovyclass Test { 
        static void main(args) { 
            // I.  Receive the thinning of a default parameter         //  Define closure variables         def closure = { 
                println "Accept One Arguments : ${it}"        }        //  Call closure         closure.call("Hello");        closure("Hello");        // II.  A closure that does not accept any parameters         //  Define closure variables  ,  Parameters are not allowed to be passed in         def closure2 = { 
     ->            println "Not Accept Arguments"        }        //  An error will be reported when the parameter is passed in         //closure2("Hello")        //  Call closure  ,  Cannot pass in parameters         closure2.call();        closure2();        // III.  Receive a closure of a custom parameter         //  Define closure variables  ,  Declare a parameter  a        def closure3 = { 
     a ->            println "${a}"        }        //  Call closure  ,  Cannot pass in parameters         closure3.call(1);        closure3(2);        // IV.  Receive the closure of two custom parameters         //  Define closure variables  ,  Declare two parameters  a, b        //  Print these two parameters in the closure         def closure4 = { 
     a, b ->            println "${a} : ${b}"        }        //  Call closure  ,  Cannot pass in parameters         closure4.call(1, 2);        closure4(3, 4);        // V.  Specify default values for closure parameters         //  Define closure variables  ,  Declare two parameters  a, b        //  And specify the default value for the closure         def closure5 = { 
     a = 0, b = "Groovy" ->            println "${a} : ${b}"        }        //  Closures have default values  ,  You can call without passing in parameters         closure5()    }}

Execution results :

Accept One Arguments : HelloAccept One Arguments : HelloNot Accept ArgumentsNot Accept Arguments121 : 23 : 40 : Groovy

 Insert picture description here

原网站

版权声明
本文为[Programmer community]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140632099499.html