当前位置:网站首页>[groovy] closure (closure call | closure default parameter it | code example)
[groovy] closure (closure call | closure default parameter it | code example)
2022-07-05 04:41:00 【Programmer community】
One 、 Call closure
perform Closure Variable Of call() Method , You can call the closure ;
// Define closure variables def closure = {
println "Hello Closure!" } // Call closure closure.call()
Execute the above code , Print Hello Closure!
;
direct stay Closure After a variable , Write a bracket , You can also call closures ;
// Define closure variables def closure = {
println "Hello Closure!" } // Call closure closure()
Execute the above code , Print Hello Closure!
;
Two 、 Closure default parameters it
Closure Closure Default OK Receive a default parameter , The parameter variable name is it ,
- If No parameter passed in , Then it for null ,
- If Pass in the parameter , The it Variable is the parameter value that should be passed in ;
stay closure() Invocation time , Pass in a parameter , It will be automatically assigned to it Variable ;
// Define closure variables def closure = {
println it } // Call closure , And pass in a default parameter closure("Hello")
The above code will print Hello
;
3、 ... and 、 Code example
Code example :
class Test {
static void main(args) {
// Define closure variables def closure = {
println "Hello Closure!" } // I. Print closure type // Print closure variable types println closure.getClass() // II. Call closure // Call closure 1 closure.call() // Call closure 2 closure() // III. Closure default parameters // Define closure variables def closure1 = {
println it } // Call closure closure1.call("Hello") // Call closure , And pass in a default parameter closure1("Closure") }}
Execution results :
class Test$_main_closure1Hello Closure!Hello Closure!HelloClosure
边栏推荐
- You Li takes you to talk about C language 7 (define constants and macros)
- 包 类 包的作用域
- [uniapp] system hot update implementation ideas
- How to force activerecord to reload a class- How do I force ActiveRecord to reload a class?
- Components in protective circuit
- TPG x AIDU | AI leading talent recruitment plan in progress!
- Official announcement! The third cloud native programming challenge is officially launched!
- 官宣!第三届云原生编程挑战赛正式启动!
- Thematic information | carbon, carbon neutrality, low carbon, carbon emissions - 22.1.9
- Leetcode 222 number of nodes of complete binary tree
猜你喜欢
Burpsuite grabs app packets
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
TPG x AIDU|AI领军人才招募计划进行中!
函数(基本:参数,返回值)
次小生成树
Setting up redis cluster cluster under Windows
Special information | real estate and office buildings - 22.1.9
如何优雅的获取每个分组的前几条数据
Invalid bound statement (not found) in idea -- problem solving
OWASP top 10 vulnerability Guide (2021)
随机推荐
Components in protective circuit
官宣!第三届云原生编程挑战赛正式启动!
American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
直播预告 | 容器服务 ACK 弹性预测最佳实践
Group counting notes (1) - check code, original complement multiplication and division calculation, floating point calculation
Setting up redis cluster cluster under Windows
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
Label exchange experiment
Data security -- 14 -- Analysis of privacy protection governance
English topic assignment (27)
Variable category (automatic, static, register, external)
SPI read / write flash principle + complete code
机器学习 --- 决策树
Stage experience
Web开发人员应该养成的10个编程习惯
Basic analysis of IIC SPI protocol
MacBook installation postgresql+postgis
[Business Research Report] top ten trends of science and technology and it in 2022 - with download link
This is an age of uncertainty
Fluent objects and lists