lua-单例
function newAccount(initlizedBanlance)
 local self = {balance = initlizedBanlance}
 local show = function (v)
     self.balance = self.balance - v
 end 

  local getBanlance = function ()
 return self.balance
 end 
 return  {
           show = show
           getBanlance = getBanlance 
 }
end 

acc = newAccount (200)
print (acc.getBanlance())
acc.show (100)

print (acc.getBanlance())


--[[
 单例方法展示
]]

function newObject(value )

 return function (action ,v )
 if  action == "get" then 
     return value 
   elseif action == "set"then 
       value = v
   else 
      error ("invalid action")
 end 
 end 
end 

d = newObject (0)
print (d ("get"))
d ("set", 10)
print (d("get"))

posted on 2017-04-28 12:13  yutingliuyl  阅读(220)  评论(0编辑  收藏  举报