Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("OOP Lua!")
- -------------------------------------------------------
- local mt = {}
- -- setter
- function mt.__newindex( self, index, value )
- if index == 'name' and type(value) == 'string' then
- self.setName(value)
- else
- rawset( self, index, value )
- end
- end
- -------------------------------------------------------
- --getter
- function mt.__index( self, index )
- if index == 'name' then
- return self.getName()
- else
- return rawget( self, index )
- end
- end
- -------------------------------------------------------
- local User = {}
- function User.new()
- local _name = 'Qugurun'
- local object = {
- setName = function(n)
- _name = n
- end,
- getName = function()
- return _name
- end
- }
- return setmetatable(object, mt)
- end
- --=====================================================
- local user1 = User.new()
- local user2 = User.new()
- user1.name = "Laimadoo"
- print(user1.name) --> Laimadoo
- print(user2.name) --> Qugurun
- print(user2._name) --> nil
- for k,v in pairs(user1) do
- print(k,v) --> getName function: ...
- --> setName function: ...
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement