当前位置:网站首页>[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
边栏推荐
- Serpentine matrix
- Sword finger offer 04 Search in two-dimensional array
- 【thingsboard】替换首页logo的方法
- Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
- English topic assignment (27)
- Setting up redis cluster cluster under Windows
- 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)
- How to get the first few pieces of data of each group gracefully
- 49 pictures and 26 questions explain in detail what is WiFi?
- American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
猜你喜欢
The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality
mxnet导入报各种libcudart*.so、 libcuda*.so找不到
CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
Solution of circular dependency
Aperçu en direct | Services de conteneurs ACK flexible Prediction Best Practices
Cookie learning diary 1
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
SPI read / write flash principle + complete code
Sword finger offer 04 Search in two-dimensional array
Wenet: E2E speech recognition tool for industrial implementation
随机推荐
Sword finger offer 04 Search in two-dimensional array
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
概率论与数理统计考试重点复习路线
How can CIOs use business analysis to build business value?
介绍汉明距离及计算示例
Decimal to hexadecimal
Download the details and sequence of the original data access from the ENA database in EBI
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)
【thingsboard】替换首页logo的方法
User behavior collection platform
Leetcode hot topic Hot 100 day 33: "subset"
PR video clip (project packaging)
Live broadcast preview | container service ack elasticity prediction best practice
Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
Qt蓝牙:搜索蓝牙设备的类——QBluetoothDeviceDiscoveryAgent
SQL set operation
A solution to the problem that variables cannot change dynamically when debugging in keil5
Introduction to RT thread kernel (5) -- memory management
[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
[Chongqing Guangdong education] National Open University 2047t commercial bank operation and management reference test in autumn 2018