Groovy delegate, owner, this references.


/ Published in: Groovy
Save to your folder(s)

This, owner, and delegate:
this : as in Java, this refers to the instance of the enclosing class where a Closure is defined
owner : the enclosing object (this or a surrounding closure)
delegate : by default the same as owner, but changeable.
(http://groovy.codehaus.org/Closures).


Copy this code and paste it in your HTML
  1. class Class1 {
  2. def closure = {
  3. println delegate.class.name
  4. def nestedClos = {
  5. println owner.class.name
  6. }
  7. nestedClos()
  8. }
  9. }
  10.  
  11. def clos = new Class1().closure
  12. clos.delegate = this
  13. clos()
  14. /*prints:
  15.  Class1
  16.  Script1
  17.  Class1$_closure1 */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.