要想了解Groovy闭包中的this,owner和delegate的含义,首先我们需要知道闭包能在哪些上下文中进行创建。
创建闭包的上下文
首先,闭包可以在方法体中创建(类的实例方法或者静态方法均可)
class Person{
def static method(){
def methodClosure={
println "methodClosure this:"+this
println "methodClosure owner:"+owner
println "methodClosure delegate:"+delegate
}
methodClosure
}
}
Person.method().call()
输出:
methodClosure this:class test.Person
methodClosure owner:class test.Person
methodClosure delegate:class test.Person
其次, 闭包可以在类中直接定义:
class Person{
def static classClosure={
println "classClosure this:"+this
println "classClosure owner:"+owner
println "classClosure delegate:"+delegate
}
}
Person.classClosure.call()
输出:
classClosure this:class test.Person
classClosure owner:class test.Person
classClosure delegate:class test.Person
再者,闭包可以在groovy的script中直接定义,实际上也是在类中直接定义,同上,因为groovy的script实际上会被编译为Script.class
def scriptClosure={
println "scriptClosure this:"+this
println "scriptClosure owner:"+owner
println "scriptClosure delegate:"+delegate
}
scriptClosure.call()
输出:
scriptClosure this:test.Script@a09e41
scriptClosure owner:test.Script@a09e41
scriptClosure delegate:test.Script@a09e41
最后, 闭包可以在闭包中定义:
def closure={
def closureClosure={
println "closureClosure this:"+this
println "closureClosure owner:"+owner
println "closureClosure delegate:"+delegate
}
closureClosure.call()
}
closure.call()
输出:
closureClosure this:test.Script@27cd63
closureClosure owner:test.Script$_run_closure2@746ad0
closureClosure delegate:test.Script$_run_closure2@746ad0
从上可以看到,闭包可以在四种上下文中进行定义,但是闭包中的this,owner 和 delegate的含义却各不相同。
静态闭包和实例闭包
实际上,闭包根据其创建的上下文不同,还可以分为静态闭包和实例闭包,在这两种情况下,this,owner和delegate的含义也是不同的。
class ClosureTest{
def static classClosure={
println "classClosure this:"+this
println "classClosure owner:"+owner
println "classClosure delegate:"+delegate
}
def instanceClosure={
println "instanceClosure this:"+this
println "instanceClosure owner:"+owner
println "instanceClosure delegate:"+delegate
}
def static classMethodClosure(){
def classMethodClosure={
println "classMethodClosure this:"+this
println "classMethodClosure owner:"+owner
println "classMethodClosure delegate:"+delegate
}
classMethodClosure.call()
}
def instanceMethodClosure(){
def instanceMethodClosure={
println "instanceMethodClosure this:"+this
println "instanceMethodClosure owner:"+owner
println "instanceMethodClosure delegate:"+delegate
}
instanceMethodClosure.call()
}
}
ClosureTest.classClosure()
new ClosureTest().instanceClosure()
ClosureTest.classMethodClosure()
new ClosureTest().instanceMethodClosure()
输出:
classClosure this:class test.ClosureTest
classClosure owner:class test.ClosureTest
classClosure delegate:class test.ClosureTest
instanceClosure this:test.ClosureTest@11d3226
instanceClosure owner:test.ClosureTest@11d3226
instanceClosure delegate:test.ClosureTest@11d3226
classMethodClosure this:class test.ClosureTest
classMethodClosure owner:class test.ClosureTest
classMethodClosure delegate:class test.ClosureTest
instanceMethodClosure this:test.ClosureTest@ecb3f1
instanceMethodClosure owner:test.ClosureTest@ecb3f1
instanceMethodClosure delegate:test.ClosureTest@ecb3f1
This在闭包中的含义
对于this来讲,它基本上保持了跟java中this一样的含义(在java的静态方法以及静态域中,this是没有任何含义的),在上面的闭包创建的4种上下文中,实际上可以了解为只有2种,一种是在普通的类中定义,如上面的Person类,一种是在groovy script中定义,实际上也是在类中定义,只不过这个是一个比较特殊的类而已(Groovy会将groovy script编译为Script.class), 所以,this在闭包中的含义指的是,表示定义该闭包的类的实例对象(实例闭包)或者类本身(静态闭包)。
Owner在闭包中的含义
对于owner来讲,它的含义基本上跟this的含义一样,只是除了一种情况,如果该闭包是在其他的闭包中定义的,那么owner是指向定义它的闭包对象。 如上面最后一种创建上下文:
closureClosure owner:test.Script$_run_closure2@746ad0
Delegate在闭包中的含义
对于delegate来讲,它的含义大多数情况下是跟owner的含义一样,除非它被显示的修改(通过Closure.setDelegate()方法进行修改)。
在上面的几种创建上下文中,可以看到,如果闭包的delegate没有被显示改动的话,那么delegate确实是同owner是一个含义。下面我们看看修改delegate的情况:
def scriptClosure={
println "scriptClosure this:"+this
println "scriptClosure owner:"+owner
println "scriptClosure delegate:"+delegate
}
println "before setDelegate()"
scriptClosure.call()
scriptClosure.setDelegate ("abc")
println "after setDelegate()"
scriptClosure.call()
输出:
before setDelegate()
scriptClosure this:test.Script@67c1a6
scriptClosure owner:test.Script@67c1a6
scriptClosure delegate:test.Script@67c1a6
after setDelegate()
scriptClosure this:test.Script@67c1a6
scriptClosure owner:test.Script@67c1a6
scriptClosure delegate:abc
其中,delegate可以被设置为任意一个对象。
下面可以看下groovy中使用了setDelegate的应用:
String.metaClass.doTestClosure={
println "doTestClosure this:"+this
println "doTestClosure owner:"+owner
println "doTestClosure delegate:"+delegate
}
"do test".doTestClosure()
Integer.metaClass.doTestClosure={
println "doTestClosure this:"+this
println "doTestClosure owner:"+owner
println "doTestClosure delegate:"+delegate
}
1.doTestClosure()
输出:
doTestClosure this:test.Script@ffeef1
doTestClosure owner:test.Script@ffeef1
doTestClosure delegate:do test
doTestClosure this:test.Script@ffeef1
doTestClosure owner:test.Script@ffeef1
doTestClosure delegate:1
可以看到,在通过metaClass动态添加方法时,delegate都被动态的设置为了调用者的实例本身,如上面的"do test"字符窜,以及整数1.
setDelegate还被广泛的使用于groovy builder的构建中。有兴趣的可以看下groovy中BuildSupport的实现。
分享到:
相关推荐
- 类`Preson2`的`fun()`方法中的闭包`classClouse`也遵循相同的规则,`this`指向`Preson2`的实例,而`owner`和`delegate`都是`Preson2`的实例。 理解闭包的这三个关键变量对于高效地利用Groovy的闭包功能至关重要。...
- **委托链**:当在闭包中调用一个方法或访问属性,Groovy会检查 `this`、`owner` 和 `delegate`,按顺序寻找方法的实现。这使得闭包能够灵活地利用上下文中的方法。 6. **闭包的应用** - 闭包在Groovy中广泛应用...
Groovy 的闭包支持闭包方法的委托,Groovy 的闭包有 `thisObject`、`owner` 和 `delegate` 三个属性,当在闭包中调用方法时,由他们来确定哪个对象来处理。 闭包委托在 Gradle 中的应用非常广泛,例如在 DSL 中,...
此外,Groovy 的 Closures 还有其他高级特性,如 `delegate` 和 `owner`,这使得它们在处理对象间关系时更为灵活。 在类和继承方面,Groovy 支持多重继承,而 Java 受限于单继承。然而,Java 通过接口弥补了这一点...