当前位置:网站首页>[groovy] closure (closure as function parameter | code example)
[groovy] closure (closure as function parameter | code example)
2022-07-05 04:40:00 【Programmer community】
List of articles
- One 、 Closures as function arguments
- Two 、 Closure as a function parameter code example
One 、 Closures as function arguments
Make a statement fun function , Sure Declare the parameter as Closure<?> Closure type , In this function , Execute closure content ;
Declare functions :
/** * Define a method , Receive closures as parameters , Execute the closure content in the method * @param closure * @return */def fun(Closure<?> closure) {
closure()}
When calling the above function , It only needs take Closure treat as Parameters Pass to function that will do :
fun ({
println "Closure 1" })
If Closure is the last parameter of the function , Sure Omit parenthesis , Write the closure after the function :
fun {
println "Closure 2" }
Two 、 Closure as a function parameter code example
Complete code example :
/** * Define a method , Receive closures as parameters , Execute the closure content in the method * @param closure * @return */def fun(Closure<?> closure) {
closure()}static void main(String[] args) {
fun ({
println "Closure 1" }) fun {
println "Closure 2" }}
Execution results :
Closure 1Closure 2
边栏推荐
- Error statuslogger log4j2 could not find a logging implementation
- Sword finger offer 04 Search in two-dimensional array
- 机器学习 --- 决策树
- [Business Research Report] Research Report on male consumption trends in other economic times -- with download link
- MacBook installation postgresql+postgis
- CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
- Matplotlib draws three-dimensional scatter and surface graphs
- 蛇形矩阵
- Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...
- [crampon game] MC tutorial - first day of survival
猜你喜欢
计组笔记(1)——校验码、原补码乘除计算、浮点数计算
[Business Research Report] top ten trends of science and technology and it in 2022 - with download link
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
Fonction (sujette aux erreurs)
Sequence diagram of single sign on Certification Center
Mxnet imports various libcudarts * so、 libcuda*. So not found
level18
[thingsboard] how to replace the homepage logo
Observable time series data downsampling practice in Prometheus
[Business Research Report] Research Report on male consumption trends in other economic times -- with download link
随机推荐
Neural networks and deep learning Chapter 5: convolutional neural networks reading questions
PR video clip (project packaging)
Stage experience
Neural networks and deep learning Chapter 6: Circular neural networks reading questions
Official announcement! The third cloud native programming challenge is officially launched!
[popular science] basic knowledge of thermal design: heat dissipation analysis of 5g optical devices
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)
[crampon programming] lintcode decoding Encyclopedia - 872 termination process
Machine learning decision tree
Flutter tips: various fancy nesting of listview and pageview
Minor spanning tree
CSDN正文自动生成目录
PHP读取ini文件并修改内容写入
Network security - record web vulnerability fixes
Rk3399 platform development series explanation (network debugging) 7.29 summary of network performance tools
The difference between bundle, chunk and module
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
Cookie learning diary 1
机器学习 --- 神经网络
[AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator