在Lua 中有两种比较常用的方法 实现 类似 C语言 static 变量的方法
其中利用闭合函数 是《Lua程序设计》(《Programming in Lua》)推荐用法
--利用全局变量实现 static variable
local function staic_test()
n = n or 0;
n = n + 1;
return n;
end
print(staic_test())
print(staic_test())
--利用闭合函数 (closure) 实现 static variable
local function staic_test2()
local i = 0;
return function()
i = i + 1;
return i;
end
end
staicor = staic_test2();
print(staicor())
print(staicor())