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"))
时间: 2024-10-11 00:24:52