Groovy04_闭包

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/urrjdg/article/details/79967992

1. 闭包的基本定义和调用

ls4_0.groovy



// 1. 闭包
def closure = {
    println 'grovvy'
}
println closure.getClass()      // class lsn4_0$_run_closure1
// 2. 调用闭包
closure.call()      // grovvy
closure()           // grovvy

// 3. 闭包默认能够接收一个参数
def closure1 = {
    println it
}
closure1('hello,closure')       // hello,closure

// 4. 给一个闭包指定一个参数列表
def closure2={
    //->      //  -> 前面不加东西  表示 不接收参数
    i='Grovvy',j ->
    println i + j
}
closure2('hello,closure','Zeking')  // hello,closureZeking
closure2('Zeking')                      // GrovvyZeking

// 5. 闭包的参数绑定(柯里化闭包)
def closure3={
    i,j->
        println i+ " "+j
}
def curriedClosurse = closure3.ncurry(1,'2')  //从左至右  closure3.rcurry()这个是从右到左

curriedClosurse('java')     // java 2

// 6. 方法调用闭包

def clouser4={
    i,j->
        println i + "-" + j
}
def func(clouser4){
    clouser4()
}

interface  Action1{
    void call()
}

func(){

}

func (new Action1(){

    @Override
    void call() {
        println 'call'      // call
    }
})
// 在一个对象上调用() ,表示调用这个对象上的call方法
class Action2{
    def call(){
        println 'call2'
    }
}

new Action2()()     // call2  等于 new Action2().call()

2. this owner delegate

ls4_1.groovy

// Groovy的 元件

def closure = {
    int i->
}

// 1. 获取闭包的参数列表
println closure.parameterTypes              // [int]  参数类型
println closure.maximumNumberOfParameters   // 1

// 2. this owner delegate
def closure2 = {
    println "this is" +this             // this is      ls4_1@1184ab05
    println "owner is"+ owner           // owner is     ls4_1@1184ab05
    println "delegate is"+ delegate     // delegate is  ls4_1@1184ab05
                // 在这种情况下是一样的
}
closure2()

class TestClosure{
    def closure2 = {
        println "this is " +this             // this is     TestClosure@22a637e7
        println "owner is "+ owner           // owner is    TestClosure@22a637e7
        println "delegate is "+ delegate     // delegate is TestClosure@22a637e7

    }
}
new TestClosure().closure2()

class TestClosure2{
    // static 他就不是一个TestClosure2的实例对象了,而是TestClosure2这个类的对象(TestClosure2.class)
    // 这时候这3个任然一样
    def static closure2 = {
        println "this is " +this             // this is     TestClosure2
        println "owner is "+ owner           // owner is    TestClosure2
        println "delegate is "+ delegate     // delegate is TestClosure2

    }
}
TestClosure2.closure2()

class TestClosure3{
    def closure1 = {
        def closure2 = {
            println "this is " +this             // this is     TestClosure3@6e4784bc
            println "owner is "+ owner           // owner is    TestClosure3$_closure1@34b7ac2f
            println "delegate is "+ delegate     // delegate is TestClosure3$_closure1@34b7ac2f
        }
        closure2()
    }
}
new TestClosure3().closure1()
// 闭包中的 this 指的是定义它的时候 所在的类的this,静态闭包当中为class
// owner 定义它的时候 所在类的对象
// delegate 默认就是owner
// owner(拥有者)没法改变,但是我们可以修改delegate(代理)

def func(){
    println "func"
}

def closure3 = {
    func()
}

closure3()          // func

class TestFun{
    def func2(){
        println 'TestFun  func'
    }
}

def closure4 = {
    func2()     //  报错,不能直接调用func2  因为不在同一个类里面

}
closure4.delegate = new TestFun()  // 所以我们就可以通过改变这个闭包的代理的方式,来让他能够调用 fun2
closure4()                     // TestFun  func


class TestFun5{
    def fun5(){
        println 'TestFun5 :  fun5'
    }
}

def fun5(){
    println "ls4_1 : fun5"
}

def closure5 = {
    def func5 = {
        println "closure5 : fun5"
    }
    fun5()
}
closure5.delegate = new TestFun5()
//closure5.resolveStrategy = Closure.DELEGATE_FIRST  // 默认是  Closure.OWNER_FIRST  --》ls4_1 : fun5
                                                   //  Closure.DELEGATE_FIRST  -》TestFun5 :  fun5
                                                    // OWNER_ONLY 只在OWNER找
                                                    // DELEGATE_ONLY 只在代理找
                                                    // TO_SELF 只在自身找 就是在 closure5 闭包内部找(最没有意义的) --> closure5 : fun5
closure5()      // ls4_1 : fun5  为什么调用的是 ls4_1 : fun5?
                // 因为闭包有一个,代理策略
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页