//闭包的三个重要变量: this owner delegate
def scriptClouser = {
println "scriptClouser this: " + this //代表闭包定义处的类
println "scriptClouser owner: " + owner //代表闭包定义处的类或对象
println "scriptClouser delegate: " + delegate //代表任意对象
}
scriptClouser.call()
//定义了一个内部类
class Person {
def static classClouser = {
println "classClouser this: " + this //代表闭包定义处的类
println "classClouser owner: " + owner //代表闭包定义处的类或对象 (闭包可以定义在闭包内,此时为对象)
println "classClouser delegate: " + delegate //代表任意对象
}
def static say() {
def methodClouser = {
println "methodClouser this: " + this //代表闭包定义处的类
println "methodClouser owner: " + owner //代表闭包定义处的类或对象
println "methodClouser delegate: " + delegate //代表任意对象
}
classClouser.call()
}
}
println "-----------------"
Person.classClouser.call()
println "-----------------"
Person.say()
结果
scriptClouser this: com.cjt.groovy.variable.004clouserstudy@6682e6a5
scriptClouser owner: com.cjt.groovy.variable.004clouserstudy@6682e6a5
scriptClouser delegate: com.cjt.groovy.variable.004clouserstudy@6682e6a5
-----------------
classClouser this: class com.cjt.groovy.variable.Person
classClouser owner: class com.cjt.groovy.variable.Person
classClouser delegate: class com.cjt.groovy.variable.Person
-----------------
classClouser this: class com.cjt.groovy.variable.Person
classClouser owner: class com.cjt.groovy.variable.Person
classClouser delegate: class com.cjt.groovy.variable.Person
上例子中如果去年类中的static
//定义了一个内部类
class Person {
def classClouser = {
println "classClouser this: " + this //代表闭包定义处的类
println "classClouser owner: " + owner //代表闭包定义处的类或对象 (闭包可以定义在闭包内,此时为对象)
println "classClouser delegate: " + delegate //代表任意对象
}
def say() {
def methodClouser = {
println "methodClouser this: " + this //代表闭包定义处的类
println "methodClouser owner: " + owner //代表闭包定义处的类或对象
println "methodClouser delegate: " + delegate //代表任意对象
}
classClouser.call()
}
}
//println "-----------------"
//Person.classClouser.call()
//println "-----------------"
//Person.say()
//如果上面去年static, Person p = new Person 用p.classClouser.call()
println "-----------------"
Person p = new Person()
p.classClouser.call()
println "-----------------"
p.say()
结果是都一样的,指向类实例
classClouser this: com.cjt.groovy.variable.Person@1192c925
classClouser owner: com.cjt.groovy.variable.Person@1192c925
classClouser delegate: com.cjt.groovy.variable.Person@1192c925
-----------------
classClouser this: com.cjt.groovy.variable.Person@1192c925
classClouser owner: com.cjt.groovy.variable.Person@1192c925
classClouser delegate: com.cjt.groovy.variable.Person@1192c925
如果是闭包中定义闭包 三个变量才会不一样
def nestClouser = {
def innerClouser = {
println "innerClouser this: " + this //com.cjt.groovy.variable.clouserstudy@74834afd
println "innerClouser owner: " + owner //com.cjt.groovy.variable.clouserstudy$_run_closure12@62cba181
println "innerClouser delegate: " + delegate //com.cjt.groovy.variable.clouserstudy$_run_closure12@62cba181
}
//innerClouser.delegate = p //修改默认的delegate
innerClouser.call()
}
nestClouser.call()
结果
innerClouser this: com.cjt.groovy.variable.004clouserstudy@22048bd6
innerClouser owner: com.cjt.groovy.variable.004clouserstudy$_run_closure12@5cc1bf20
innerClouser delegate: com.cjt.groovy.variable.004clouserstudy$_run_closure12@5cc1bf20
网友评论