以下示例改编自“Groovy in Action”
class Mother {
Closure birth() {
def closure = { caller ->
[this, caller]
}
return closure
}
}
Mother julia = new Mother()
closure = julia.birth()
context = closure.call(this)
println context[0].class.name // Will print the name of the Script class
assert context[1] instanceof Script
根据书,this
闭包内部的值是最外层范围(即julia
声明的范围)。我是否正确地假设
this
在闭包内部计算闭包被调用的范围?- 在上面显示的闭包内,
this
并caller
引用相同的范围?
谢谢,唐