【Groovy】闭包 Closure ( 闭包类 Closure 简介 | this、owner、delegate 成员区别 | 静态闭包变量 | 闭包中定义闭包 )(二)

简介: 【Groovy】闭包 Closure ( 闭包类 Closure 简介 | this、owner、delegate 成员区别 | 静态闭包变量 | 闭包中定义闭包 )(二)

分析生成的字节码文件 :


//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
import groovy.lang.Closure;
import groovy.lang.GroovyObject;
import groovy.lang.MetaClass;
import groovy.transform.Generated;
import groovy.transform.Internal;
import org.codehaus.groovy.runtime.GeneratedClosure;
import org.codehaus.groovy.runtime.callsite.CallSite;
public class Test2 implements GroovyObject {
    private Object closure2;
    @Generated
    public Test2() {
        CallSite[] var1 = $getCallSiteArray();
        super();
        // 创建外层闭包 
        Test2._closure1 var2 = new Test2._closure1(this, this);
        this.closure2 = var2;
        MetaClass var3 = this.$getStaticMetaClass();
        this.metaClass = var3;
    }
    @Generated
    @Internal
    public MetaClass getMetaClass() {
        MetaClass var10000 = this.metaClass;
        if (var10000 != null) {
            return var10000;
        } else {
            this.metaClass = this.$getStaticMetaClass();
            return this.metaClass;
        }
    }
    @Generated
    @Internal
    public void setMetaClass(MetaClass var1) {
        this.metaClass = var1;
    }
    @Generated
    public Object getClosure2() {
        return this.closure2;
    }
    @Generated
    public void setClosure2(Object var1) {
        this.closure2 = var1;
    }
  // 这是外层的 Closure 闭包
    public final class _closure1 extends Closure implements GeneratedClosure {
        public _closure1(Object _outerInstance, Object _thisObject) {
            CallSite[] var3 = $getCallSiteArray();
            super(_outerInstance, _thisObject);
        }
        public Object doCall(Object it) {
            CallSite[] var2 = $getCallSiteArray();
    // 这是内层的 Closure 闭包
            final class _closure2 extends Closure implements GeneratedClosure {
                public _closure2(Object _outerInstance, Object _thisObject) {
                    CallSite[] var3 = $getCallSiteArray();
                    super(_outerInstance, _thisObject);
                }
                public Object doCall(Object it) {
                    CallSite[] var2 = $getCallSiteArray();
                    var2[0].callCurrent(this, var2[1].call("this : ", this.getThisObject()));
                    var2[2].callCurrent(this, var2[3].call("owner : ", var2[4].callGroovyObjectGetProperty(this)));
                    return var2[5].callCurrent(this, var2[6].call("delegate : ", var2[7].callGroovyObjectGetProperty(this)));
                }
                @Generated
                public Object doCall() {
                    CallSite[] var1 = $getCallSiteArray();
                    return this.doCall((Object)null);
                }
            }
    // 创建内层闭包时 , 传入的 this 是 外层闭包的 this.getThisObject() 
    // 因此 this 值仍是 Test2 实例对象 
    // owner、delegate 变为外层的 Closure 闭包 ; 
            Object closure3 = new _closure2(this, this.getThisObject());
            return var2[0].call(closure3);
        }
        @Generated
        public Object doCall() {
            CallSite[] var1 = $getCallSiteArray();
            return this.doCall((Object)null);
        }
    }
}






三、 完整代码示例


完整代码示例 :


class Test2 {
    // 定义静态闭包
    // 即可以通过类执行
    // 又可以通过对象执行
    def static closure = {
        println "this : " + this
        println "owner : " + owner
        println "delegate : " + delegate
    }
    // 闭包中定义闭包
    def closure2 = {
        def closure3 = {
            println "this : " + this
            println "owner : " + owner
            println "delegate : " + delegate
        }
        closure3()
    }
}
println "通过类执行闭包 :"
Test2.closure()
println "\n通过对象执行闭包 :"
new Test2().closure()
println "\n闭包中定义闭包并执行 : "
new Test2().closure2()



执行结果 :


通过类执行闭包 :
this : class Test2
owner : class Test2
delegate : class Test2
通过对象执行闭包 :
this : class Test2
owner : class Test2
delegate : class Test2
闭包中定义闭包并执行 : 
this : Test2@1f010bf0
owner : Test2$_closure1@40db2a24
delegate : Test2$_closure1@40db2a24

image.png


目录
相关文章
|
5月前
|
JavaScript 前端开发 数据安全/隐私保护
闭包对于保护私有变量和函数的作用
JavaScript中的闭包用于创建私有作用域,保护变量和函数不被外部直接访问。它们实现封装和信息隐藏,防止全局命名冲突,确保数据安全和稳定性。闭包还支持访问控制和持久状态保持,常用于模块化、数据隐藏等,增强代码的可维护性、可重用性和安全性。
|
5月前
|
缓存 JavaScript 前端开发
|
3月前
|
自然语言处理 JavaScript 前端开发
理解闭包的定义
【7月更文挑战第10天】闭包是编程中的关键概念,特别是函数式编程中。它是函数及其相关引用环境的组合,能访问词法作用域内外的变量,即使外部函数已执行完毕。闭包提供封装私有变量、保持状态、延迟执行的功能,常用于模块化、函数工厂、模拟私有方法和回调。JavaScript中通过函数嵌套实现闭包,但也可能导致内存泄漏和性能问题。
84 2
|
5月前
|
人工智能 自然语言处理 前端开发
闭包是什么?闭包的用途是什么?
闭包是什么?闭包的用途是什么?
【Groovy】闭包 Closure ( 闭包类 Closure 简介 | this、owner、delegate 成员赋值及源码分析 )
【Groovy】闭包 Closure ( 闭包类 Closure 简介 | this、owner、delegate 成员赋值及源码分析 )
163 0
【Groovy】闭包 Closure ( 闭包类 Closure 简介 | this、owner、delegate 成员赋值及源码分析 )
【Groovy】闭包 Closure ( 闭包类 Closure 简介 | 闭包 parameterTypes 和 maximumNumberOfParameters 成员用法 )
【Groovy】闭包 Closure ( 闭包类 Closure 简介 | 闭包 parameterTypes 和 maximumNumberOfParameters 成员用法 )
112 0
【Groovy】闭包 Closure ( 闭包类 Closure 简介 | 闭包 parameterTypes 和 maximumNumberOfParameters 成员用法 )
【Groovy】闭包 Closure ( 闭包定义 | 闭包类型 | 查看编译后的字节码文件中的闭包类型变量 )
【Groovy】闭包 Closure ( 闭包定义 | 闭包类型 | 查看编译后的字节码文件中的闭包类型变量 )
145 0
【Groovy】闭包 Closure ( 闭包定义 | 闭包类型 | 查看编译后的字节码文件中的闭包类型变量 )
【Groovy】闭包 Closure ( 闭包调用 | 闭包默认参数 it | 代码示例 )
【Groovy】闭包 Closure ( 闭包调用 | 闭包默认参数 it | 代码示例 )
155 0
【Groovy】闭包 Closure ( 闭包调用 | 闭包默认参数 it | 代码示例 )
|
机器学习/深度学习
【Groovy】闭包 Closure ( 闭包参数绑定 | curry 函数 | rcurry 函数 | ncurry 函数 | 代码示例 )(二)
【Groovy】闭包 Closure ( 闭包参数绑定 | curry 函数 | rcurry 函数 | ncurry 函数 | 代码示例 )(二)
124 0
【Groovy】闭包 Closure ( 闭包参数绑定 | curry 函数 | rcurry 函数 | ncurry 函数 | 代码示例 )(二)
【Groovy】闭包 Closure ( 自定义闭包参数 | 自定义单个闭包参数 | 自定义多个闭包参数 | 闭包参数默认值指定 )
【Groovy】闭包 Closure ( 自定义闭包参数 | 自定义单个闭包参数 | 自定义多个闭包参数 | 闭包参数默认值指定 )
232 0
【Groovy】闭包 Closure ( 自定义闭包参数 | 自定义单个闭包参数 | 自定义多个闭包参数 | 闭包参数默认值指定 )