Groovy 添加新属性时是否参考Expando对象?

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

以下示例来自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
    at hello$_run_closure1.doCall(hello.groovy:5)
    at hello.run(hello.groovy:6)

显然,第5行中的
this
不是指
boxer
对象,而是指脚本。那么,向Expando
boxer
添加
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)