用空代理实现account(lua程序设计21.7练习21.4题)
function createAccount()
local proxy = {}
local mt = {balance = 0}
mt.withdraw = function(v) mt.balance = mt.balance - v end
mt.deposit = function(v) mt.balance = mt.balance + v end
mt.getBalance = function() return mt.balance end
setmetatable(proxy,mt)
mt.__index = mt
return proxy
end
local account = createAccount()
account.deposit(300)
print(account.getBalance())
account.withdraw(100)
print(account.getBalance())