简体   繁体   English

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

[英]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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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