首页 > 其他 > 详细

lua实现单例模式

时间:2016-02-01 18:49:01      阅读:294      评论:0      收藏:0      [点我收藏+]
Singleton = {}
function Singleton:new(o)
    o = o or {}
    setmetatable(o,self)
    self.__index = self
    return o
end

function Singleton:Instance()
    if self.instance == nil then
        self.instance = self:new()
    end
    return self.instance
end


s1 = Singleton:Instance()

s2 = Singleton:Instance()

if s1 == s2 then
    print("两个对象是相同的实例")
end

 

lua实现单例模式

原文:http://www.cnblogs.com/damowang/p/5175843.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!