fork download
  1. -- create class
  2. Account = {balance = 0}
  3.  
  4.  
  5. -- constructor
  6. function Account:new (o)
  7. o = o or {}
  8. setmetatable(o, self)
  9. self.__index = self
  10. return o
  11. end
  12.  
  13. -- method `deposit'
  14. function Account:deposit (v)
  15. self.balance = self.balance + v
  16. end
  17.  
  18. -- method `withdraw'
  19. function Account:withdraw (v)
  20. if v > self.balance then error"insuficient funds" end
  21. self.balance = self.balance - v
  22. end
  23.  
  24.  
  25. -- use example
  26. a = Account:new()
  27. print(a.balance) --> 0
  28. a:deposit(1000.00)
  29. a:withdraw(100.00)
  30. print(a.balance) --> 900
Success #stdin #stdout 0.02s 2540KB
stdin
Standard input is empty
stdout
0
900