知识大全 Groovy解惑——closure中的owner
Posted 知
篇首语:常勤精进,譬如水长流,则能穿石。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Groovy解惑——closure中的owner相关的知识,希望对你有一定的参考价值。
Groovy解惑——closure中的owner 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
本篇将讲解一下closure中的owner 以及this delegate以及owner三者间的关系
先让我们来看个例子
class OwnerDemo def outerClosure = println the owner of outerClosure: + owner def innerClosure = println the owner of innerClosure: + owner def innestClosure = println the owner of innestClosure: + owner innestClosure() innerClosure() def ownerDemo = new OwnerDemo() ownerDemo outerClosure()
运行结果
the owner of outerClosure: OwnerDemo@eccfe
the owner of innerClosure: OwnerDemo$_closure @ cf
the owner of innestClosure: OwnerDemo$_closure _closure @ dcbb
注意 OwnerDemo$_closure 指的是outerClosure的类名 而OwnerDemo$_closure _closure 指的是innerClosure的类名
通过这个例子 大家就清楚了 closure的owner引用的是该closure的 拥有者
那么this delegate以及owner有什么关系呢?
隐式变量delegate的默认值为owner
如果closure没有 嵌套 在其他closure中 那么该closure的owner的值为this;
否则该closure的owner引用的是 直接包含 该closure的closure
让我们用事实来说话吧
class OwnerDemo def outerClosure = println the owner of outerClosure: + owner println the delegate of outerClosure: + delegate println this in the outerClosure: + this def innerClosure = println the owner of innerClosure: + owner println the delegate of innerClosure: + delegate println this in the innerClosure: + this def innestClosure = println the owner of innestClosure: + owner println the delegate of innestClosure: + delegate println this in the innestClosure: + this println innestClosure: + innestClosure innestClosure() println innerClosure: + innerClosure innerClosure() def ownerDemo = new OwnerDemo() def outerClosure = ownerDemo outerClosure println outerClosure: + outerClosure outerClosure()
运行结果
outerClosure: OwnerDemo$_closure @ cc b
the owner of outerClosure: OwnerDemo@ e f
the delegate of outerClosure: OwnerDemo@ e f
this in the outerClosure: OwnerDemo@ e f
innerClosure: OwnerDemo$_closure _closure @ eb db
the owner of innerClosure: OwnerDemo$_closure @ cc b
the delegate of innerClosure: OwnerDemo$_closure @ cc b
this in the innerClosure: OwnerDemo@ e f
innestClosure: OwnerDemo$_closure _closure _closure @ a ee
the owner of innestClosure: OwnerDemo$_closure _closure @ eb db
the delegate of innestClosure: OwnerDemo$_closure _closure @ eb db
this in the innestClosure: OwnerDemo@ e f
大家可以从其中值的关系看出this delegate以及owner三者的关系与我们之前所说的相符 :)
cha138/Article/program/Java/hx/201311/25968相关参考
Delphi中Owner和Parent的区别 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! Pa
我国是乙肝病毒感染的高发地区。根据全国范围的调查,约有60%的人群已接触过乙肝病毒,其中50%没有造成感染,另有10%血清中的乙肝表面抗原(HBsAg)为持续阳性,提示有慢性的乙肝病毒感染。持续HBs
Groovy轻松入门—搭建Groovy开发环境 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 由
安装Groovy的Eclipse插件 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!Groovy的E
Groovy编程动态改变对象的能力 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 从Groovy
Groovy:沉睡的巨人 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 像RubyPython甚
Groovy编程—Annotations的使用 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 从
Groovy编程——Mixin的使用 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 由于Java
用Groovy搞定SOAP消息 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 看了YanHua同
Java帝国的王储——Groovy 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!让我们来回顾一下主