当前位置:网站首页>[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】
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 ;
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
边栏推荐
- [moteur illusoire UE] il ne faut que six étapes pour réaliser le déploiement du flux de pixels ue5 et éviter les détours! (4.26 et 4.27 principes similaires)
- flutter 对象和列表
- OWASP top 10 vulnerability Guide (2021)
- [phantom engine UE] the difference between running and starting, and the analysis of common problems
- WeNet:面向工业落地的E2E语音识别工具
- CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
- Raki's notes on reading paper: code and named entity recognition in stackoverflow
- C26451: arithmetic overflow: use the operator * on a 4-byte value, and then convert the result to an 8-byte value. To avoid overflow, cast the value to wide type before calling the operator * (io.2)
- Basic analysis of IIC SPI protocol
- Neural network and deep learning Chapter 1: introduction reading questions
猜你喜欢
How should programmers learn mathematics
Key review route of probability theory and mathematical statistics examination
MySQL in-depth learning - index creation and deletion, index design principles, index failure scenarios, query optimization, index push down ICP
C26451: arithmetic overflow: use the operator * on a 4-byte value, and then convert the result to an 8-byte value. To avoid overflow, cast the value to wide type before calling the operator * (io.2)
托管式服务网络:云原生时代的应用体系架构进化
About the prompt loading after appscan is opened: guilogic, it keeps loading and gets stuck. My personal solution. (it may be the first solution available in the whole network at present)
American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
How to get the first few pieces of data of each group gracefully
Function (error prone)
windows下Redis-cluster集群搭建
随机推荐
English topic assignment (27)
Web开发人员应该养成的10个编程习惯
Setting up redis cluster cluster under Windows
次小生成树
SPI read / write flash principle + complete code
Matplotlib draws three-dimensional scatter and surface graphs
Network security - record web vulnerability fixes
[phantom engine UE] only six steps are needed to realize the deployment of ue5 pixel stream and avoid detours! (the principles of 4.26 and 4.27 are similar)
CSDN正文自动生成目录
Wan broadband access technology V EPON Technology
Rk3399 platform development series explanation (network debugging) 7.29 summary of network performance tools
How to carry out "small step reconstruction"?
The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality
Variable category (automatic, static, register, external)
2022-2028 global and Chinese equipment as a Service Market Research Report
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
解密函数计算异步任务能力之「任务的状态及生命周期管理」
Hexadecimal to decimal
Machine learning decision tree
jmeter -- 分布式压测