Groovy解惑——closure中的owner
来源:互联网 发布:2016奥运会网络转播权 编辑:程序博客网 时间:2024/04/29 08:12
在上篇《Groovy解惑——closure中的delegate》中,我帮大家讲解了delegate是什么以及其作用。
本篇将讲解一下closure中的owner,以及this,delegate以及owner三者间的关系。
先让我们来看个例子:
通过这个例子,大家就清楚了,closure的owner引用的是该closure的‘拥有者’
那么this, delegate以及owner有什么关系呢?
隐式变量delegate的默认值为owner,
如果closure没有‘嵌套’在其他closure中,那么该closure的owner的值为this;
否则该closure的owner引用的是‘直接包含’该closure的closure
让我们用事实来说话吧:
连夜连发2篇文章以回报‘蛟龙居’的常客 :)
本篇将讲解一下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()
运行结果: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@eccfe7
the owner of innerClosure: OwnerDemo$_closure1@4745cf
the owner of innestClosure: OwnerDemo$_closure1_closure2@109dcbb
注意:OwnerDemo$_closure1指的是outerClosure的类名,而OwnerDemo$_closure1_closure2指的是innerClosure的类名the owner of innerClosure: OwnerDemo$_closure1@4745cf
the owner of innestClosure: OwnerDemo$_closure1_closure2@109dcbb
通过这个例子,大家就清楚了,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()
运行结果: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$_closure1@10cc9b4
the owner of outerClosure: OwnerDemo@8e7f54
the delegate of outerClosure: OwnerDemo@8e7f54
this in the outerClosure: OwnerDemo@8e7f54
innerClosure: OwnerDemo$_closure1_closure2@1eb1db2
the owner of innerClosure: OwnerDemo$_closure1@10cc9b4
the delegate of innerClosure: OwnerDemo$_closure1@10cc9b4
this in the innerClosure: OwnerDemo@8e7f54
innestClosure: OwnerDemo$_closure1_closure2_closure3@12a78ee
the owner of innestClosure: OwnerDemo$_closure1_closure2@1eb1db2
the delegate of innestClosure: OwnerDemo$_closure1_closure2@1eb1db2
this in the innestClosure: OwnerDemo@8e7f54
大家可以从其中值的关系看出this, delegate以及owner三者的关系与我们之前所说的相符 :)the owner of outerClosure: OwnerDemo@8e7f54
the delegate of outerClosure: OwnerDemo@8e7f54
this in the outerClosure: OwnerDemo@8e7f54
innerClosure: OwnerDemo$_closure1_closure2@1eb1db2
the owner of innerClosure: OwnerDemo$_closure1@10cc9b4
the delegate of innerClosure: OwnerDemo$_closure1@10cc9b4
this in the innerClosure: OwnerDemo@8e7f54
innestClosure: OwnerDemo$_closure1_closure2_closure3@12a78ee
the owner of innestClosure: OwnerDemo$_closure1_closure2@1eb1db2
the delegate of innestClosure: OwnerDemo$_closure1_closure2@1eb1db2
this in the innestClosure: OwnerDemo@8e7f54
连夜连发2篇文章以回报‘蛟龙居’的常客 :)
- Groovy解惑——closure中的owner
- Groovy解惑——closure中的delegate
- Groovy基础——Closure(闭包)详解
- Groovy基础——Closure(闭包)详解
- Groovy基础——Closure(闭包)详解
- Groovy基础——Closure(闭包)详解
- Groovy基础——Closure(闭包)详解
- Groovy闭包中的this,owner和delegate
- Groovy 之 Closure
- Groovy Closure简介
- groovy之closure
- Python 中的闭包 —— Closure
- 4.Groovy闭包中的this,owner和delegate的含义
- New Groovy --- Closure/Block问题
- Groovy中closure的delegate
- 解惑—Linux中的地址空间
- 解惑—Linux中的地址空间
- 解惑—Linux中的地址空间(一)
- C# 编码规范和编程好习惯
- 哭了
- 团队开发规范
- Groovy学习之资料篇
- Groovy解惑——closure中的delegate
- Groovy解惑——closure中的owner
- Groovy高效编程——用Gant替代Ant(更新于2008.02.18)
- 基本命令(文件系统管理)- -[转]
- Groovy全攻略--嵌入篇
- 程序员必备,程序员四大忌
- Groovy全攻略--运行篇
- Websharp 10 - Websharp Framework
- Linux文件系统概述 [转]
- 关于linux图形界面的基本知识[转贴,新手必读]- -