Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function newObject()
- local object = {}
- object.somePublicVar = 100
- local protected = {money = 0}
- function object:setName(name)
- self.name = name
- end
- function object:addMoney(amount)
- protected.money = protected.money + amount
- end
- function object:getMoney()
- return protected.money
- end
- return object, protected
- end
- local function newChild()
- local object, protected = newObject()
- function object:spendMoney(amount)
- protected.money = protected.money - amount
- end
- local parent_setName = object.setName
- function object:setName(name)
- name = 'Mr. ' .. name
- parent_setName(self, name)
- end
- return object
- end
- local myChild = newChild()
- myChild:setName('Nyashka')
- print(myChild.name) -- Mr. Nyashka
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement