当前位置:网站首页>[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 3Content ;
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 4Two 、 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 53、 ... 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
边栏推荐
- Practice | mobile end practice
- File upload bypass summary (upload labs 21 customs clearance tutorial attached)
- 机器学习 --- 神经网络
- windows下Redis-cluster集群搭建
- [AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator
- The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality
- You Li takes you to talk about C language 7 (define constants and macros)
- Aperçu en direct | Services de conteneurs ACK flexible Prediction Best Practices
- Hexadecimal to decimal
- Convert Boolean to integer value PHP - Convert Boolean to integer value PHP
猜你喜欢

Fonction (sujette aux erreurs)
![[thingsboard] how to replace the homepage logo](/img/65/5296c26f975d79d65d36c2e76e47c1.png)
[thingsboard] how to replace the homepage logo

Aperçu en direct | Services de conteneurs ACK flexible Prediction Best Practices

Invalid bound statement (not found) in idea -- problem solving

WeNet:面向工业落地的E2E语音识别工具

OWASP top 10 vulnerability Guide (2021)

揭秘技术 Leader 必备的七大清奇脑回路

mxnet导入报各种libcudart*.so、 libcuda*.so找不到

Network security - record web vulnerability fixes

QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent
随机推荐
线上故障突突突?如何紧急诊断、排查与恢复
电源管理总线 (PMBus)
Cookie learning diary 1
函数(基本:参数,返回值)
How should programmers learn mathematics
机器学习 --- 决策树
【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)
Data security -- 14 -- Analysis of privacy protection governance
American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
Thematic information | carbon, carbon neutrality, low carbon, carbon emissions - 22.1.9
How to get the first few pieces of data of each group gracefully
Neural networks and deep learning Chapter 4: feedforward neural networks reading questions
Minor spanning tree
美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
English topic assignment (27)
Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
Learning MVVM notes (1)
Ffmepg usage guide
All in one 1413: determine base
A survey of automatic speech recognition (ASR) research