【Groovy】闭包 Closure ( 闭包类 Closure 简介 | this、owner、delegate 成员区别 | 静态闭包变量 | 闭包中定义闭包 )
文章目錄
- 總結
- 一、靜態閉包變量
- 1、執行普通閉包變量
- 2、執行靜態閉包變量
- 二、 在閉包中定義閉包
- 三、 完整代碼示例
總結
在閉包中 , 打印 this , owner , delegate , 打印結果都是創建閉包時所在的類 ;
- 如果在類中創建閉包 , 則打印結果是類 ;
- 如果在實例對象中創建閉包 , 則打印結果是實例對象 ;
- 如果在閉包 A 中創建 閉包 B , this 是最外層閉包 A 之外的類 , owner , delegate 是上一層閉包 B ;
一、靜態閉包變量
1、執行普通閉包變量
在類中定義閉包變量 , 在閉包中打印 this、owner、delegate 值 ,
class Test2 {def closure = {println "this : " + thisprintln "owner : " + ownerprintln "delegate : " + delegate} }執行上述 Test2 類中的閉包 ,
new Test2().closure()打印結果如下 : 打印的值都是 Test2 實例對象 ;
this : Test2@5082d622 owner : Test2@5082d622 delegate : Test2@5082d6222、執行靜態閉包變量
如果將閉包聲明為靜態變量 ,
class Test2 {def static closure = {println "this : " + thisprintln "owner : " + ownerprintln "delegate : " + delegate} }直接使用閉包所在類直接調用閉包 , 不再使用閉包所在類對象調用閉包 ;
Test2.closure()執行結果為 : 打印的值都是 Test2 類 ;
this : class Test2 owner : class Test2 delegate : class Test2還是上述靜態閉包變量 , 使用 Test2 實例對象調用 ,
new Test2().closure()打印的結果是創建閉包時所在的類 ;
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()} }打印結果如下 :
this : Test2@291a7e3c owner : Test2$_closure1@4ae9cfc1 delegate : Test2$_closure1@4ae9cfc1this 值為 外部的 Test2 實例對象 ;
owner 和 delegate 是 Test2 中定義的 closure2 閉包 ;
創建 closure2 閉包時 , this、owner、delegate 都是 Test2 實例對象 ;
但是創建 closure3 閉包時 , this 的值還是設置 closure2 的 this 值 , owner、delegate 值設置成 closure2 閉包 ;
// 創建內層閉包時 , 傳入的 this 是 外層閉包的 this.getThisObject() // 因此 this 值仍是 Test2 實例對象 // owner、delegate 變為外層的 Closure 閉包 ; Object closure3 = new _closure2(this, this.getThisObject());分析生成的字節碼文件 :
// // 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();// 創建外層閉包 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();// 這是內層的 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);}}// 創建內層閉包時 , 傳入的 this 是 外層閉包的 this.getThisObject() // 因此 this 值仍是 Test2 實例對象 // owner、delegate 變為外層的 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 {// 定義靜態閉包// 即可以通過類執行// 又可以通過對象執行def static closure = {println "this : " + thisprintln "owner : " + ownerprintln "delegate : " + delegate}// 閉包中定義閉包def closure2 = {def closure3 = {println "this : " + thisprintln "owner : " + ownerprintln "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總結
以上是生活随笔為你收集整理的【Groovy】闭包 Closure ( 闭包类 Closure 简介 | this、owner、delegate 成员区别 | 静态闭包变量 | 闭包中定义闭包 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Groovy】闭包 Closure (
- 下一篇: 【错误记录】Groovy 闭包使用报错