Caught: java.lang.StackOverflowError
来源:4-9 闭包进阶讲解

qq_夜_71
2019-12-22
def outer = {
def inner = {
println "this: ${this} \n owner :${owner} \ndelegate :${delegate}"
}
inner.call()
}
outer.call()
结果: Caught: java.lang.StackOverflowError .
为什么?
写回答
2回答
-
def outer = {
def inner = {
println "this: ${this.getClass()} "
println "owner:${owner.getClass()}"
println "delegate:${delegate.getClass()}"
}
inner.call()
}
outer.call()212019-12-27 -
慕勒4503557
2021-06-15
当一个闭包嵌入在GString的情况下,它的
toString()
是不会被调用的,你贴的那段代码中的owner和delegate是闭包outer,所以它的toString()
不会被调用。你可以如下改动:def inner = { println "this: ${this} \n owner :${owner.toString()} \ndelegate :${delegate.toString()}" }
PS:this不需要toString()
00