当前位置:网站首页>[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

 Insert picture description here

原网站

版权声明
本文为[Programmer community]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140632099662.html