如何在groovy中访问委托对象属性?

时间:2017-08-01 01:11:54

标签: groovy

这是我对此问题的示例代码:

class Person {
    String fullName
}

def myClosure = {
    fullName = "Chakroun Anas"
}

Person person = new Person()
myClosure.delegate = person
myClosure()
println(person.fullName)

这是输出:

null

那么可以从闭包中访问委托对象属性吗?如果是,那怎么样?

提前致谢。

1 个答案:

答案 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)