当前位置:网站首页>[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
边栏推荐
- Neural network and deep learning Chapter 1: introduction reading questions
- [crampon game] MC tutorial - first day of survival
- Setting up redis cluster cluster under Windows
- Hexadecimal to decimal
- Pointer function (basic)
- PHP reads the INI file and writes the modified content
- windows下Redis-cluster集群搭建
- Sword finger offer 04 Search in two-dimensional array
- 函数(易错)
- 蛇形矩阵
猜你喜欢
Mxnet imports various libcudarts * so、 libcuda*. So not found
计组笔记(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)
[thingsboard] how to replace the homepage logo
Function (basic: parameter, return value)
函數(易錯)
Live broadcast preview | container service ack elasticity prediction best practice
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
[AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator
随机推荐
Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
windows下Redis-cluster集群搭建
这是一个不确定的时代
Stage experience
Scope of package class package
Live broadcast preview | container service ack elasticity prediction best practice
Sequence diagram of single sign on Certification Center
Function overloading
Data security -- 14 -- Analysis of privacy protection governance
The principle of attention mechanism and its application in seq2seq (bahadanau attention)
Neural networks and deep learning Chapter 3: linear model reading questions
[crampon programming] lintcode decoding Encyclopedia - 1100 strange printer
程序员应该怎么学数学
防护电路中的元器件
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
Observable time series data downsampling practice in Prometheus
How should programmers learn mathematics
Sword finger offer 04 Search in two-dimensional array
Variable category (automatic, static, register, external)
函数(易错)