简体   繁体   中英

How to access delegate object properties in groovy?

This is my example code for this question:

class Person {
    String fullName
}

def myClosure = {
    fullName = "Chakroun Anas"
}

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

This is the output :

null

So is it possible to access the delegate object properties from the closure ? if so then how ?

Thanks in advance.

Consider this (using this page as a reference):

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM