当前位置:网站首页>[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
边栏推荐
- Invalid bound statement (not found) in idea -- problem solving
- Flink集群配置
- 【科普】热设计基础知识:5G光器件之散热分析
- 线上故障突突突?如何紧急诊断、排查与恢复
- Pointer function (basic)
- Flutter tips: various fancy nesting of listview and pageview
- 机器学习 --- 决策树
- 指针函数(基础)
- 函数(易错)
- 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)
猜你喜欢
Function (basic: parameter, return value)
WeNet:面向工业落地的E2E语音识别工具
What are the building energy-saving software
首席信息官如何利用业务分析构建业务价值?
Observable time series data downsampling practice in Prometheus
windows下Redis-cluster集群搭建
质量体系建设之路的分分合合
The principle of attention mechanism and its application in seq2seq (bahadanau attention)
How can CIOs use business analysis to build business value?
取余操作是一个哈希函数
随机推荐
Neural networks and deep learning Chapter 5: convolutional neural networks reading questions
指针函数(基础)
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)
质量体系建设之路的分分合合
Cookie learning diary 1
10 programming habits that web developers should develop
How to carry out "small step reconstruction"?
Sword finger offer 04 Search in two-dimensional array
首席信息官如何利用业务分析构建业务价值?
Learning MVVM notes (1)
[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)
[AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator
2022-2028 global and Chinese video coding and transcoding Market Research Report
次小生成树
English topic assignment (26)
Ffmepg usage guide
Basic analysis of IIC SPI protocol
JVM 原理和流程简介
Error statuslogger log4j2 could not find a logging implementation
Neural network and deep learning Chapter 1: introduction reading questions