Lua单例
manager = {} -- 单例类
manager.num = 1
manager.__index = manager

-- 构造
function manager:new()
    local self = {}

    setmetatable(self, manager)
    return self
end

-- 函数 获取当前类的对象唯一入口
function manager:getinstance()
    if self.instance == nil then
        self.instance = manager:new()
    end
    return self.instance
end

m1 = manager:getinstance()
m1.num = 10
m2 = manager:getinstance()
m2.num = 20

print(m1.num)
print(m2.num)
上一篇
下一篇