首页 > 代码库 > cocos2d-x-lua基础系列教程五(lua单例)

cocos2d-x-lua基础系列教程五(lua单例)

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 = http://www.mamicode.com/v"invalid action")
 end 
 end 
end 

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

cocos2d-x-lua基础系列教程五(lua单例)