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 classassert context[1] instanceof Script
根据这本书,封闭内部的价值是最外面的范围(即宣布朱利亚的范围).我正确地假设
>这个内部的一个闭包可以评估到关闭的范围吗?
>在上面所示的关闭中,这个和调用者是否参考相同的范围?
谢谢,
唐
…this refers to the closure,not to
the declaring object. At this point,
closures play a trick for us. They
delegate all method calls to a
so-called delegate object,which by
default happends to be the declaring
object (that is,the owner). This make
the closure appear as if the enclosed
code runs in the birthday context.
对于你的问题
this insIDe a closure evaluates to the scope in which the closure is called?
从书中他们说“这是指封闭,而不是声明对象”
但是从bertport和我的实验看,“这”实际上是声明对象.
无论哪种方式,您的问题的答案仍然是“否”.
within the closure shown above,this and caller refer to the same scope?
恐怕不是.
请注意,Groovy in Action中的第143和144页需要进行一些更正
http://groovy.canoo.com/errata/erratum/show/5
http://groovy.canoo.com/errata/erratum/show/8
总结以上是内存溢出为你收集整理的Groovy:封闭中的’this’的含义全部内容,希望文章能够帮你解决Groovy:封闭中的’this’的含义所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)