这是我对此问题的示例代码:
class Person {
String fullName
}
def myClosure = {
fullName = "Chakroun Anas"
}
Person person = new Person()
myClosure.delegate = person
myClosure()
println(person.fullName)
这是输出:
null
那么可以从闭包中访问委托对象属性吗?如果是,那怎么样?
提前致谢。
答案 0 :(得分:0)
考虑这一点(使用this page作为参考):
class Person {
String fullName
}
def myClosure = {
fullName = "Chakroun Anas"
}
Person person = new Person()
myClosure.resolveStrategy = Closure.DELEGATE_FIRST
myClosure.delegate = person
myClosure()
println(person.fullName)