当前位置:网站首页>[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
2022-07-05 04:40:00 【Programmer community】
List of articles
- summary
- One 、 Interface call() Method
- Two 、 Definition in class call() Method
- 3、 ... and 、 Complete code example
summary
stay Use after instance object " () " Bracket symbol , Indicates that the instance object is called " call() " Method ;
One 、 Interface call() Method
Definition Action Interface , In this interface , establish void call() Abstract method ;
/** * Create an interface * Interface call Method * Call above receive Closure as a parameter fun Function time * Pass in the Action Anonymous inner class */interface Action {
void call()}
Create the above Action Anonymous inner class of method , and Use () Execute the above anonymous inner class object , Meeting Automatically call Action Anonymous inner class call Method ;
// stay Action Use after object () Executing a method is equivalent to calling call Method new Action(){
@Override void call() {
println "Closure 3" }}()
Execute the above code , prints
Closure 3
Content ;
At the same time, the above anonymous inner class , Can be used as a closure , Pass to
/** * Define a method , Receive closures as parameters , Execute the closure content in the method * @param closure * @return */def fun(closure) {
closure()}
function ; towards fun Function , Pass in Action Anonymous inner class , When this function is executed , Execute closure content , Automatically called Action Anonymous inner class call Method ;
// towards fun Function , Pass in Action Anonymous inner class // When this function is executed , Execute closure content , Automatically called Action Anonymous inner class call Method fun (new Action(){
@Override void call() {
println "Closure 3" }})
Above fun Function execution result :
Closure 4
Two 、 Definition in class call() Method
In ordinary Groovy Class , Definition call() Method ;
// Define a have call Class of method class Action2 {
def call() {
println "Closure 5" }}
After the instance object of this class Use () , Will automatically execute this class call Method ;
// Use after the instance object of this class () , Will automatically execute this class call Method new Action2()()
The execution result is :
Closure 5
3、 ... and 、 Complete 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()}/** * Create an interface * Interface call Method * Call above receive Closure as a parameter fun Function time * Pass in the Action Anonymous inner class */interface Action {
void call()}// take Closure treat as Parameters Pass to function fun ({
println "Closure 1"})// Closure is the last parameter of the function , Sure Omit parenthesis , Write the closure after the function fun {
println "Closure 2"}// stay Action Use after object () Executing a method is equivalent to calling call Method new Action(){
@Override void call() {
println "Closure 3" }}()// towards fun Function , Pass in Action Anonymous inner class // When this function is executed , Execute closure content , Automatically called Action Anonymous inner class call Method fun (new Action(){
@Override void call() {
println "Closure 4" }})// Define a have call Class of method class Action2 {
def call() {
println "Closure 5" }}// Use after the instance object of this class () , Will automatically execute this class call Method new Action2()()
Execution results :
Closure 1Closure 2Closure 3Closure 4Closure 5
边栏推荐
- [thingsboard] how to replace the homepage logo
- Flink集群配置
- Variable category (automatic, static, register, external)
- Ffmepg usage guide
- 包 类 包的作用域
- [finebi] the process of making custom maps using finebi
- [phantom engine UE] package error appears! Solutions to findpin errors
- 2022-2028 global and Chinese FPGA prototype system Market Research Report
- Here comes the Lantern Festival red envelope!
- Network security - record web vulnerability fixes
猜你喜欢
MySQL in-depth learning - index creation and deletion, index design principles, index failure scenarios, query optimization, index push down ICP
A survey of automatic speech recognition (ASR) research
CSDN正文自动生成目录
【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)
托管式服务网络:云原生时代的应用体系架构进化
[crampon game] MC tutorial - first day of survival
Reading and visualization of DICOM, MHD and raw files in medical imaging
American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
SPI read / write flash principle + complete code
MacBook installation postgresql+postgis
随机推荐
防护电路中的元器件
Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)
After the deployment of web resources, the navigator cannot obtain the solution of mediadevices instance (navigator.mediadevices is undefined)
Function template
Cookie learning diary 1
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)
[phantom engine UE] package error appears! Solutions to findpin errors
Network security - record web vulnerability fixes
函数(基本:参数,返回值)
线上故障突突突?如何紧急诊断、排查与恢复
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)
Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
WeNet:面向工业落地的E2E语音识别工具
[crampon game] MC tutorial - first day of survival
How to carry out "small step reconstruction"?
电源管理总线 (PMBus)
[finebi] the process of making custom maps using finebi
[uniapp] system hot update implementation ideas
Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...