lua closure factory 完整代码示例?
时间: 2023-04-11 09:01:44 浏览: 140
以下是一个简单的 Lua 闭包工厂的完整代码示例:
function createClosureFactory()
local count = 0
return function()
count = count + 1
return count
end
end
-- 使用闭包工厂创建两个闭包
local closure1 = createClosureFactory()
local closure2 = createClosureFactory()
-- 调用闭包获取计数器的值
print(closure1()) -- 输出 1
print(closure1()) -- 输出 2
print(closure2()) -- 输出 1
print(closure1()) -- 输出 3
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)