【Groovy】闭包 Closure ( 闭包类 Closure 简介 | this、owner、delegate 成员区别 | 静态闭包变量 | 闭包中定义闭包 )
文章目錄
- 總結(jié)
- 一、靜態(tài)閉包變量
- 1、執(zhí)行普通閉包變量
- 2、執(zhí)行靜態(tài)閉包變量
- 二、 在閉包中定義閉包
- 三、 完整代碼示例
總結(jié)
在閉包中 , 打印 this , owner , delegate , 打印結(jié)果都是創(chuàng)建閉包時(shí)所在的類 ;
- 如果在類中創(chuàng)建閉包 , 則打印結(jié)果是類 ;
- 如果在實(shí)例對(duì)象中創(chuàng)建閉包 , 則打印結(jié)果是實(shí)例對(duì)象 ;
- 如果在閉包 A 中創(chuàng)建 閉包 B , this 是最外層閉包 A 之外的類 , owner , delegate 是上一層閉包 B ;
一、靜態(tài)閉包變量
1、執(zhí)行普通閉包變量
在類中定義閉包變量 , 在閉包中打印 this、owner、delegate 值 ,
class Test2 {def closure = {println "this : " + thisprintln "owner : " + ownerprintln "delegate : " + delegate} }執(zhí)行上述 Test2 類中的閉包 ,
new Test2().closure()打印結(jié)果如下 : 打印的值都是 Test2 實(shí)例對(duì)象 ;
this : Test2@5082d622 owner : Test2@5082d622 delegate : Test2@5082d6222、執(zhí)行靜態(tài)閉包變量
如果將閉包聲明為靜態(tài)變量 ,
class Test2 {def static closure = {println "this : " + thisprintln "owner : " + ownerprintln "delegate : " + delegate} }直接使用閉包所在類直接調(diào)用閉包 , 不再使用閉包所在類對(duì)象調(diào)用閉包 ;
Test2.closure()執(zhí)行結(jié)果為 : 打印的值都是 Test2 類 ;
this : class Test2 owner : class Test2 delegate : class Test2還是上述靜態(tài)閉包變量 , 使用 Test2 實(shí)例對(duì)象調(diào)用 ,
new Test2().closure()打印的結(jié)果是創(chuàng)建閉包時(shí)所在的類 ;
this : class Test2 owner : class Test2 delegate : class Test2二、 在閉包中定義閉包
在 Test2 類中定義 閉包變量 closure2 , 在 closure2 閉包中定義 closure3 閉包 ,
class Test2 {def closure2 = {def closure3 = {println "this : " + thisprintln "owner : " + ownerprintln "delegate : " + delegate}closure3()} }打印結(jié)果如下 :
this : Test2@291a7e3c owner : Test2$_closure1@4ae9cfc1 delegate : Test2$_closure1@4ae9cfc1this 值為 外部的 Test2 實(shí)例對(duì)象 ;
owner 和 delegate 是 Test2 中定義的 closure2 閉包 ;
創(chuàng)建 closure2 閉包時(shí) , this、owner、delegate 都是 Test2 實(shí)例對(duì)象 ;
但是創(chuàng)建 closure3 閉包時(shí) , this 的值還是設(shè)置 closure2 的 this 值 , owner、delegate 值設(shè)置成 closure2 閉包 ;
// 創(chuàng)建內(nèi)層閉包時(shí) , 傳入的 this 是 外層閉包的 this.getThisObject() // 因此 this 值仍是 Test2 實(shí)例對(duì)象 // owner、delegate 變?yōu)橥鈱拥?Closure 閉包 ; Object closure3 = new _closure2(this, this.getThisObject());分析生成的字節(jié)碼文件 :
// // 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;@Generatedpublic Test2() {CallSite[] var1 = $getCallSiteArray();super();// 創(chuàng)建外層閉包 Test2._closure1 var2 = new Test2._closure1(this, this);this.closure2 = var2;MetaClass var3 = this.$getStaticMetaClass();this.metaClass = var3;}@Generated@Internalpublic MetaClass getMetaClass() {MetaClass var10000 = this.metaClass;if (var10000 != null) {return var10000;} else {this.metaClass = this.$getStaticMetaClass();return this.metaClass;}}@Generated@Internalpublic void setMetaClass(MetaClass var1) {this.metaClass = var1;}@Generatedpublic Object getClosure2() {return this.closure2;}@Generatedpublic 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();// 這是內(nèi)層的 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)));}@Generatedpublic Object doCall() {CallSite[] var1 = $getCallSiteArray();return this.doCall((Object)null);}}// 創(chuàng)建內(nèi)層閉包時(shí) , 傳入的 this 是 外層閉包的 this.getThisObject() // 因此 this 值仍是 Test2 實(shí)例對(duì)象 // owner、delegate 變?yōu)橥鈱拥?Closure 閉包 ; Object closure3 = new _closure2(this, this.getThisObject());return var2[0].call(closure3);}@Generatedpublic Object doCall() {CallSite[] var1 = $getCallSiteArray();return this.doCall((Object)null);}} }三、 完整代碼示例
完整代碼示例 :
class Test2 {// 定義靜態(tài)閉包// 即可以通過(guò)類執(zhí)行// 又可以通過(guò)對(duì)象執(zhí)行def static closure = {println "this : " + thisprintln "owner : " + ownerprintln "delegate : " + delegate}// 閉包中定義閉包def closure2 = {def closure3 = {println "this : " + thisprintln "owner : " + ownerprintln "delegate : " + delegate}closure3()} }println "通過(guò)類執(zhí)行閉包 :"Test2.closure()println "\n通過(guò)對(duì)象執(zhí)行閉包 :"new Test2().closure()println "\n閉包中定義閉包并執(zhí)行 : "new Test2().closure2()執(zhí)行結(jié)果 :
通過(guò)類執(zhí)行閉包 : this : class Test2 owner : class Test2 delegate : class Test2通過(guò)對(duì)象執(zhí)行閉包 : this : class Test2 owner : class Test2 delegate : class Test2閉包中定義閉包并執(zhí)行 : this : Test2@1f010bf0 owner : Test2$_closure1@40db2a24 delegate : Test2$_closure1@40db2a24總結(jié)
以上是生活随笔為你收集整理的【Groovy】闭包 Closure ( 闭包类 Closure 简介 | this、owner、delegate 成员区别 | 静态闭包变量 | 闭包中定义闭包 )的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【Groovy】闭包 Closure (
- 下一篇: 【错误记录】Groovy 闭包使用报错