hello.groovy
import groovy.lang.Closure Closure clos = { println "Hello closure"} println "delegate:" + clos.delegate.class.name; println "owner: " + clos.owner.class.name; clos.call(); println "===============================" class C { Closure clos = { println this.class.name println "delegate:" + delegate.class.name; println "owner: " + owner.class.name; def nestClos = { println "in nest delegate:" + delegate.class.name println "in nest owner:" + owner.class.name; } nestClos() } } def cls = new C().clos; cls(); println "====== afer reset delegate for outer clos =====" cls.delegate = this cls();
delegate:hello owner: hello Hello closure =============================== C delegate:C owner: C in nest delegate:C$_closure1 in nest owner:C$_closure1 ====== afer reset delegate for outer clos ===== C delegate:hello owner: C in nest delegate:C$_closure1 in nest owner:C$_closure1
原文地址:http://blog.csdn.net/cleverwyq/article/details/42871899