【发布时间】:2017-07-07 09:17:52
【问题描述】:
我想知道为什么以下两个 sn-ps 输出不同的结果。看起来迭代计数器是闭包处理的特殊情况。
int i = 1
def closures = (1..3).collect {
return { println i; ++i }
}
for (int j = 0; j < 3; ++j) {
closures += { println j }
}
closures*.call()
1
2
3
3
3
3
【问题讨论】: