当前位置:网站首页>[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
边栏推荐
- Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
- You Li takes you to talk about C language 7 (define constants and macros)
- Observable time series data downsampling practice in Prometheus
- PHP reads the INI file and writes the modified content
- [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)
- 美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
- The remainder operation is a hash function
- The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality
- Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
- 2022-2028 global and Chinese virtual data storage Market Research Report
猜你喜欢
Aperçu en direct | Services de conteneurs ACK flexible Prediction Best Practices
[phantom engine UE] realize the animation production of mapping tripod deployment
[phantom engine UE] package error appears! Solutions to findpin errors
TPG x AIDU | AI leading talent recruitment plan in progress!
Burpsuite grabs app packets
QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent
Observable time series data downsampling practice in Prometheus
CSDN body auto generate directory
Thematic information | carbon, carbon neutrality, low carbon, carbon emissions - 22.1.9
Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
随机推荐
Fonction (sujette aux erreurs)
[finebi] the process of making custom maps using finebi
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)
Matplotlib draws three-dimensional scatter and surface graphs
PHP reads the INI file and writes the modified content
Practice | mobile end practice
2022-2028 global and Chinese video coding and transcoding Market Research Report
Minor spanning tree
Basic analysis of IIC SPI protocol
Network security - record web vulnerability fixes
Here comes the Lantern Festival red envelope!
Machine learning decision tree
Wenet: E2E speech recognition tool for industrial implementation
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
Error statuslogger log4j2 could not find a logging implementation
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)
Wan broadband access technology V EPON Technology
Discussion on the dimension of confrontation subspace
Machine learning -- neural network
Construction d'un Cluster redis sous Windows