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

时间:2024-01-11 18:19:26
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"))