Original Address http://www.freecls.com/a/2712/1b
The most similar thing to objects and classes in lua is that tables have methods and properties, so classes and objects can only be simulated using tables
To state that tables are already capable of handling most scenarios, and that the lua scripting language is not an object-oriented language (its advantage is lightweight, simple and fast), so it is sometimes amazing to emulate object-oriented, so the following is for learning only and is not recommended for formal environments.
Example
--Declare a Base Class Web local Web = {name='freecls', url='http://www.freecls.com', dt='2018', server='aliyun'} function Web:say_name() print(self.name) end function Web:say_url() print(self.url) end function Web:new(name,url) local o = {} setmetatable(o,self) --take o Meta-table of__index Metamethod set to Web --that o Attributes that are not in the Web Find in Tables self.__index = self --If passed name,url Set to Table o in --If not, follow it Web Content inside if name ~= nil then o.name = name end if url ~= nil then o.url = url end return o end local w1 = Web:new('Cang Lang Shui') --Cang Lang Shui http://www.freecls.com 2018 aliyun print(w1.name, w1.url, w1.dt, w1.server) w1:say_name() --Cang Lang Shui local w2 = Web:new('Dai Lei', 'http://www.freecls.com/u/info/2712') --Dai Lei http://www.freecls.com/u/info/2712 2018 aliyun print(w2.name, w2.url, w2.dt, w2.server) w2:say_name() --Dai Lei
Simulated Inheritance
--Declare a Base Class Web --Notice this Web The table has changed a lot to be inherited local Web = {name='freecls', url='http://www.freecls.com', dt='2018', server='aliyun'} function Web:say_name() print(self.name) end function Web:say_url() print(self.url) end function Web:new(name,url) local o = {} setmetatable(o,self) --take o Meta-table of__index Metamethod set to Web --that o Attributes that are not in the Web Find in Tables self.__index = self if name ~= nil then o.name = name else o.name = self.name end if url ~= nil then o.url = url else o.url = self.url end o.dt = self.dt o.server = self.server o.say_name = self.say_name o.say_url = self.say_url return o end local Web1 = {age=22,sex='male'} function Web1:how_old() print(self.age) end --Simulated Inheritance function Web1:new(age,sex) local o = Web:new() setmetatable(o, self) self.__index = self return o end local w1 = Web1:new() w1.name = 'Cang Lang Shui' --22 male Cang Lang Shui http://www.freecls.com 2018 aliyun print(w1.age, w1.sex, w1.name, w1.url, w1.dt, w1.server) w1:say_url() --http://www.freecls.com w1:say_name() --Cang Lang Shui w1:how_old() --22
Analog Rewrite
local Web = {name='freecls', url='http://www.freecls.com', dt='2018', server='aliyun'} function Web:say_name() print(self.name) end function Web:say_url() print(self.url) end function Web:new(name,url) local o = {} setmetatable(o,self) --take o Meta-table of__index Metamethod set to Web --that o Attributes that are not in the Web Find in Tables self.__index = self if name ~= nil then o.name = name else o.name = self.name end if url ~= nil then o.url = url else o.url = self.url end o.dt = self.dt o.server = self.server o.say_name = self.say_name o.say_url = self.say_url return o end local Web1 = {age=22,sex='male'} function Web1:how_old() print(self.age) end --This method will fail and the override will fail function Web1:say_name() print(self.name..' Rewrite') end --Simulated Inheritance function Web1:new(age,sex) local o = Web:new() setmetatable(o, self) self.__index = self --Rewrite succeeded function o:say_name() print(self.name..' Rewrite') end return o end local w1 = Web1:new() w1.name = 'Cang Lang Shui' --22 male Cang Lang Shui http://www.freecls.com 2018 aliyun print(w1.age, w1.sex, w1.name, w1.url, w1.dt, w1.server) w1:say_url() --http://www.freecls.com w1:say_name() --Cang Lang Shui Rewrite w1:how_old() --22
summary
1. This article is only a brief introduction to lua simulation object-oriented, if you have questions, you can leave a message for me
2.lua version 5.1, running environment centos7 64 bit
3. Original Address http://www.freecls.com/a/2712/1b