当前位置:网站首页>[groovy] closure closure (customize closure parameters | customize a single closure parameter | customize multiple closure parameters | specify the default value of closure parameters)
[groovy] closure closure (customize closure parameters | customize a single closure parameter | customize multiple closure parameters | specify the default value of closure parameters)
2022-07-05 04:41:00 【Programmer community】
List of articles
- One 、 Custom closure parameter list
- 1、 Define the closure of a custom parameter
- 2、 Define closures for multiple custom parameters
- 3、 Specify default values for closure parameters
- Two 、 Complete code example
One 、 Custom closure parameter list
If you want to Closure in , Passing multiple parameters , need by Closure Specify the parameter list ;
Specify a parameter list for the closure , Need to be in closure The starting position uses " -> " Symbol , Specify the parameter list on the left side of the symbol ;
1、 Define the closure of a custom parameter
stay " -> " Write a variable to the left of the symbol a , here The variable a Can receive any type of value , This parameter can be printed in the closure a Value ;
// Define closure variables , Declare a parameter a def closure3 = {
a -> println "${a}" } // Call closure , Cannot pass in parameters closure3.call(1); closure3(2);
The print result of the above closure is :
12
2、 Define closures for multiple custom parameters
stay " -> " Write multiple variables to the left of the symbol , Variables are separated by commas , Such as a , b , At this point, the closure can receive multiple parameters , These two parameters can be of any type ;
// 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);
The result is :
1 : 23 : 4
3、 Specify default values for closure parameters
In closure , have access to Parameter name = The default value is The way , After specifying the default value for the closure parameter , When a closure is called , You can not pass in this parameter with default value ;
If Closure Parameters All have default values ,
- Sure No parameters are passed ;
- If you pass
1
1
1 Parameters , Meeting Assign values to parameters from left to right according to the default rules ;
If Closure Parameters , Some have default values , Some have no default values , Suppose there is
2
2
2 Parameters , Yes
1
1
1 Each has a default value ,
1
1
1 There is no default value ;
- If Pass on
2
2
2 Parameters , Then assign values from left to right ;
- If you deliver
1
1
1 Parameters , be Assign this value to a parameter that has no default value ;
Code example :
// 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 :
0 : Groovy
Two 、 Complete code example
Complete code example :
import org.codehaus.groovy.ant.Groovyclass Test {
static void main(args) {
// 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 :
121 : 23 : 40 : Groovy
边栏推荐
- A survey of automatic speech recognition (ASR) research
- Decimal to hexadecimal
- [Business Research Report] Research Report on male consumption trends in other economic times -- with download link
- Function template
- Rk3399 platform development series explanation (network debugging) 7.29 summary of network performance tools
- level18
- 机器学习 --- 神经网络
- Sword finger offer 07 Rebuild binary tree
- How to remove installed elpa package
- Power management bus (pmbus)
猜你喜欢
Minor spanning tree
[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
How can CIOs use business analysis to build business value?
TPG x AIDU|AI领军人才招募计划进行中!
如何优雅的获取每个分组的前几条数据
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
3 minutes learn to create Google account and email detailed tutorial!
直播预告 | 容器服务 ACK 弹性预测最佳实践
A solution to the problem that variables cannot change dynamically when debugging in keil5
托管式服务网络:云原生时代的应用体系架构进化
随机推荐
Mxnet imports various libcudarts * so、 libcuda*. So not found
PHP读取ini文件并修改内容写入
Minor spanning tree
Introduction to RT thread kernel (5) -- memory management
Thematic information | carbon, carbon neutrality, low carbon, carbon emissions - 22.1.9
Inline built-in function
Qt蓝牙:搜索蓝牙设备的类——QBluetoothDeviceDiscoveryAgent
这是一个不确定的时代
Function (basic: parameter, return value)
This is an age of uncertainty
Key review route of probability theory and mathematical statistics examination
Function (error prone)
[goweb development] Introduction to authentication modes based on cookies, sessions and JWT tokens
History of web page requests
All in one 1413: determine base
Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
Data security -- 14 -- Analysis of privacy protection governance
The principle of attention mechanism and its application in seq2seq (bahadanau attention)
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
Sword finger offer 04 Search in two-dimensional array