lua闭包

function MakeCounter()
    local t = 0
    return function()
        t = t + 1
        return t
    end
end

local func = MakeCounter()
for i = 0,10 do
    print(func())
end
local func2 = MakeCounter()
for i = 0,10 do
    print(func2())
end

>lua -e "io.stdout:setvbuf 'no'" "main.lua"
1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
>Exit code: 0

posted @ 2017-06-11 06:59  瓜蛋  阅读(220)  评论(0编辑  收藏  举报