美文网首页
Groovy:闭包(Closure)

Groovy:闭包(Closure)

作者: lv_mock | 来源:发表于2020-03-18 23:21 被阅读0次

闭包(Closure)

def closure = {
    println 'groovy'
}
println closure.getClass() //Test$_run_closure1
closure.call() //groovy
closure() //groovy
//默认一个参数
def closure = {
    println it
}
closure('Groovy') //Groovy
def closure = {
    String i,String j-> //不写参数类型也可以
        println i + j
}
closure("hello","world")
//输出:helloworld
def closure = {
    String i = "hello",String j->
        println i + j
}
closure("hello","world") //helloworld
closure("world")  //helloworld
//柯里化闭包(就是个参数绑定一个值)
def closure = {
    String i = "hello",String j->
        println i + j
}
def closure2 = closure.ncurry(0,"Groovy") //第0个参数是Groovy
closure2("Java")  //GroovyJava
def func(closure) {
    closure()
}
interface  Action {
    void call()
}
func(new Action() {
    @Override
    void call() {
        println 'call'
    }
})
//输出 call

class Action1{
    def call() {
        println 'call'
    }
}
new Action1()()
//输出 call

调用一个对象的"()"方法,就是调用了该对象上的call()方法

def closure = {
    int i,String j->
}
println closure.parameterTypes //[int, class java.lang.String]
println closure.maximumNumberOfParameters //2

this/owner/delegate

def closure = {
        println this
        println owner
        println delegate
}
closure()
//输出
//Test@e50a6f6
//Test@e50a6f6
//Test@e50a6f6

输出的是Test的一个对象,分析:查看编译后的Test.class文件

public class Test extends Script {
...
    public Object run() {
        //内部类
        class NamelessClass_1 extends Closure implements GeneratedClosure {
        ·   public NamelessClass_1(Object _outerInstance, Object _thisObject) {
                ...
                super(_outerInstance, _thisObject);
            }
        }
        //创建closure的地方
        Object closure = new NamelessClass_1(this, this);
        ...
    }
    ...
}

创建Closure的时候传递了两个参数,都是this,this就是当前的类Test,下面是创建closeure是调用的super方法

public Closure(Object owner, Object thisObject) {
    this.owner = owner;
    this.delegate = owner;
    this.thisObject = thisObject;//这个就是闭包中的this
    ...
}

owner,delegate,this都是Test这个类

class TestClosure {
    def static closure = {
        println "this : " + this
        println "owner : " + owner
        println "delegate : " + delegate
    }
}
TestClosure.closure()
//输出
//this : class TestClosure
//owner : class TestClosure
//delegate : class TestClosure

输出的是Class的对象,不是实例对象

class TestClosure {
    def closure1 = {
        def closure2 = {
            println "this : " + this
            println "owner : " + owner
            println "delegate : " + delegate
        }
        closure2()
    }
}
new TestClosure().closure1()
//输出
//this : TestClosure@2b6faea6
//owner : TestClosure$_closure1@59d016c9
//delegate : TestClosure$_closure1@59d016c9

分析编译后的文件:

public class TestClosure implements GroovyObject {
    public TestClosure() {
        ...
        TestClosure._closure1 var2 = new TestClosure._closure1(this, this);//创建closure1
        ...
    }
    
    class _closure1 extends Closure implements GeneratedClosure {
        public _closure1(Object _outerInstance, Object _thisObject) {
            ...
            super(_outerInstance, _thisObject);
        }
        public Object doCall(Object it) {
            ...
            class NamelessClass_1 extends Closure implements GeneratedClosure {
                ...
                super(_outerInstance, _thisObject);
            }
            //this就是closure1对象,this.getThisObject()就是closure1中的this,也就是TestClosure对象
            Object closure2 = new NamelessClass_1(this, this.getThisObject());
            ...
        }
    }
}
public Closure(Object owner, Object thisObject) {
    this.owner = owner;
    this.delegate = owner;
    this.thisObject = thisObject;//这个就是闭包中的this
    ...
}

TestClosure的构造方法中创建了closure1对象,在closure1中的doCall方法中创建了closure2对象,创建closure2对象的构造函数传入了this和this.getThisObject(),this.getThisObject()就是closure1中的this,也就是TestClosure对象。
this:定义它的时候 所在的类的this,静态闭包当中为class
owner:定义它的时候 所在类的对象
delegate:默认就是owner

delegate的作用

class TestFunc{
    def func() {
        print 'func'
    }
}
def closure = {
    func()
}
closure.delegate = new TestFunc()
closure()
//输出: func

修改closure的代理,在代理中找func()方法

代理策略:
class TestFunc{
    def func() {
        print 'func1'
    }
}
def func() {
    print 'func2'
}
def closure = {
    func()
}
closure.delegate = new TestFunc()
closure.resolveStrategy = Closure.DELEGATE_FIRST //去掉这行输出func2
closure()
//输出: func1

resolveStrategy:优先策略,默认为OWNER_FIRST。

相关文章

  • Groovy Closure(闭包)

    1. 语法 1.1. 定义一个闭包 闭包是Groovy中非常重要的一个数据类型或者说一种概念。闭包是一种数据类型,...

  • Groovy:闭包(Closure)

    闭包(Closure) 调用一个对象的"()"方法,就是调用了该对象上的call()方法 this/owner/d...

  • Groovy语法基础三

    上接Groovy语法基础二 六、闭包 闭包,英文叫Closure,是Groovy中非常重要的一个数据类型或者说一...

  • Groovy<第八篇>:基础语法(7)---闭包(Closure

    闭包是Groovy语言最常用的用法之一,Closure是闭包的意思。闭包是一个短的匿名代码块。它通常跨越几行代码。...

  • 关于rust中的闭包(一)

    闭包 在计算机中,闭包 Closure, 又称词法闭包 Lexical Closure 或函数闭包 functio...

  • 关于闭包

    闭包的英文是closure,又称词法闭包(Lexical Closure)和函数闭包(Function Closu...

  • 理解闭包

    闭包 何为闭包 闭包(Closure)是词法闭包(Lexical Closure)的缩写 高级程序设计中写有权访问...

  • python之闭包与装饰器

    1 闭包 维基百科给出的解析:闭包(英语:Closure),又称词法闭包(Lexical Closure)或函数闭...

  • 闭包,定时器

    问题 1.什么是闭包? 有什么作用 闭包(英语:Closure),又称词法闭包(Lexical Closure)或...

  • [Code] 优雅地使用python闭包

    在计算机科学中,闭包(英语:Closure),又称词法闭包(Lexical Closure)或函数闭包(funct...

网友评论

      本文标题:Groovy:闭包(Closure)

      本文链接:https://www.haomeiwen.com/subject/mpvhyhtx.html