Advertisement
lvs

OOP again

lvs
Aug 25th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.82 KB | None | 0 0
  1. local function newObject()
  2.     local object = {}
  3.     object.somePublicVar = 100
  4.     local protected = {money = 0}
  5.     function object:setName(name)
  6.         self.name = name
  7.     end
  8.     function object:addMoney(amount)
  9.         protected.money = protected.money + amount
  10.     end
  11.     function object:getMoney()
  12.         return protected.money
  13.     end
  14.     return object, protected
  15. end
  16.  
  17. local function newChild()
  18.     local object, protected = newObject()
  19.     function object:spendMoney(amount)
  20.         protected.money = protected.money - amount
  21.     end
  22.     local parent_setName = object.setName
  23.     function object:setName(name)
  24.         name = 'Mr. ' .. name
  25.         parent_setName(self, name)
  26.     end
  27.     return object
  28. end
  29.  
  30. local myChild = newChild()
  31. myChild:setName('Nyashka')
  32. print(myChild.name) -- Mr. Nyashka
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement