闭包中的代码可以引用it
变量。
8.times { println it }
或
def mywith(Closure closure) {
closure()
}
mywith { println it }
考虑到这种行为,您不能期望下面的代码打印0011
2.times {
println it
mywith {
println it
}
}
相反,我不得不写
2.times { i ->
println i
mywith {
println i
}
}
我的问题是:没有参数的闭包为什么要覆盖it
变量,即使它们不需要它。
https://stackoverflow.com/questions/2361650
复制相似问题