【发布时间】:2010-10-23 15:13:56
【问题描述】:
以下示例改编自“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指的是同一个范围?
谢谢, 唐
【问题讨论】: