Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
看这个测试代码:
def a = "test" def expando = new Expando() expando.a = a expando.foobar = {a} expando.a = "test1" assert expando.foobar() != a
为什么最后一个断言失败?它认为“a”是局部变量,而不是expando.a 属性。
感谢帮助
也许我弄错了,但是当您调用 时expando.foobar(),它会返回分配给 的闭包的结果foobar。在这种情况下,它是a,因此它返回 a: 的值test。
expando.foobar()
foobar
a
test
expando.foobar()不调用属性“a”,因为闭包不会查找它们的委托,除非变量未在范围内定义(在本例中是)。
编辑:如果你这样做expando.foobar = {delegate.a},那将返回你期望的结果。
expando.foobar = {delegate.a}