当前位置:网站首页>[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
边栏推荐
- Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)
- All in one 1413: determine base
- Hexadecimal to decimal
- Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
- Key review route of probability theory and mathematical statistics examination
- 官宣!第三届云原生编程挑战赛正式启动!
- 自动语音识别(ASR)研究综述
- [phantom engine UE] package error appears! Solutions to findpin errors
- 防护电路中的元器件
- 如何优雅的获取每个分组的前几条数据
猜你喜欢

2022-2028 global and Chinese equipment as a Service Market Research Report

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)

MySQL in-depth learning - index creation and deletion, index design principles, index failure scenarios, query optimization, index push down ICP
![[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording](/img/11/b55f85ef9e1f22254218cb9083b823.png)
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording

指针函数(基础)
![[phantom engine UE] package error appears! Solutions to findpin errors](/img/d5/6747e20da6a8a4ca461094bd27bbf0.png)
[phantom engine UE] package error appears! Solutions to findpin errors

10 programming habits that web developers should develop

level18

2022-2028 global and Chinese video coding and transcoding Market Research Report

托管式服务网络:云原生时代的应用体系架构进化
随机推荐
Mode in BST (binary tree & Notes on question brushing)
TPG x AIDU|AI领军人才招募计划进行中!
level17
首席信息官如何利用业务分析构建业务价值?
Leetcode hot topic Hot 100 day 33: "subset"
[Chongqing Guangdong education] National Open University 2047t commercial bank operation and management reference test in autumn 2018
Decimal to hexadecimal
Construction d'un Cluster redis sous Windows
介绍汉明距离及计算示例
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
Fonction (sujette aux erreurs)
QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent
Web开发人员应该养成的10个编程习惯
CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
Sword finger offer 07 Rebuild binary tree
Managed service network: application architecture evolution in the cloud native Era
All in one 1413: determine base
2022-2028 global and Chinese virtual data storage Market Research Report
mxnet导入报各种libcudart*.so、 libcuda*.so找不到