Groovy 添加新属性时是否参考Expando对象?
以下示例来自Groovy in Action(2007)一书的清单7.22: 我将代码放入脚本Groovy 添加新属性时是否参考Expando对象?,groovy,Groovy,以下示例来自Groovy in Action(2007)一书的清单7.22: 我将代码放入脚本hello.groovy。当我运行它时,我得到了以下错误: Caught: groovy.lang.MissingPropertyException: No such property: takeThis for class: hello groovy.lang.MissingPropertyException: No such property: takeThis for class: hello
hello.groovy
。当我运行它时,我得到了以下错误:
Caught: groovy.lang.MissingPropertyException: No such property: takeThis for class: hello
groovy.lang.MissingPropertyException: No such property: takeThis for class: hello
at hello$_run_closure1.doCall(hello.groovy:5)
at hello.run(hello.groovy:6)
显然,第5行中的
this
不是指boxer
对象,而是指脚本。那么,向Expandoboxer
添加fightBack
属性的正确方法是什么呢?将此
替换为委托
此
引用脚本(如您所述),委托
引用调用闭包的调用方
您可以在
this
、delegate
和owner
的用法上获得差异 将此
替换为委托
此
引用脚本(如您所述),委托
引用调用闭包的调用方
您可以在this
、delegate
和owner
的用法上获得差异
Caught: groovy.lang.MissingPropertyException: No such property: takeThis for class: hello
groovy.lang.MissingPropertyException: No such property: takeThis for class: hello
at hello$_run_closure1.doCall(hello.groovy:5)
at hello.run(hello.groovy:6)