-- create class

Account = {balance = 0}

-- construct

function Account:new(o)
     o = o or {}
     setmetatable(o,self)
     self.__index = self
     return o
end

function Account:deposit(v)
     self.balance = self.balance + v
end

function Account:withdraw(v)
     if v > self.balance then
          error"insuficient funds"
     end
     self.balance = self.balance -v 
end

-- use example
a = Account:new()
print(a.balance)      --> 0
a:deposit(1000.00)
a:withdraw(100.00)
print(a.balance)      --> 900

  

相关文章:

  • 2021-07-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-26
  • 2022-01-06
  • 2021-10-01
相关资源
相似解决方案