此问题在此处已有答案:
Closures in Groovy not capturing outside variables(1个答案)
四个月前关门了。
我的代码是:
def ip_list = ['192.168.1.1', '192.168.1.2', '192.168.1.3' ]
def task = [:]
for (ip in ip_list) {
task[ip] = { println "test on ${ip}" }
}
for (f in task.values())
{
f()
}
输出为:
test on 192.168.1.3
test on 192.168.1.3
test on 192.168.1.3
但我期望的输出是:
test on 192.168.1.1
test on 192.168.1.2
test on 192.168.1.3
我该怎么办?
谢谢你!
1条答案
按热度按时间bttbmeg01#
1
2
3