Object-Oriented programming is not an idea for a language. In a process-oriented language, you can use object-oriented programming. In Lua, there is no object-oriented concept, and there is no class definition or subclass definition. However, in Lua, the idea of object-oriented can also be used to implement object-oriented class inheritance.
I. Copying tables is object-oriented.
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
-- Object-oriented in Lua--[[ Table Copying Method object-oriented The parameter is a table. By traversing the value of this table, an empty table is assigned, and the newly created table is returned to clone the table.]]function clone(tab) local ins = {} for key, var in pairs(tab) do ins[key] = var end return insend --[[ Copy a table The first parameter is the target table, and the second parameter is the table to be copied. Assign a value to the target table by traversing the tab.]]function copy(dist,tab) for key, var in pairs(tab) do dist[key] = var endend -- Define an empty table, which is equivalent to a classPeople ={} --function People.sayHi()-- print("People say hi")--end -- Define the method sayhi in the class and input a self ParameterPeople.sayHi = function (self) print("People say hi:"..self.name)end -- DefinenewTo generate an object by cloning the people table.-- It is equivalent to a constructor in the class.People.new = function (name) local self = clone(People) self.name = name return selfend --local p = clone(People)--p.sayHi()-- Generate a new objectlocal p = People.new("ZhangSan")--p.sayHi(p)--p:sayHi()-- Define an empty table, which also represents a classMan = {}-- Man constructor to implement class inheritance in LuaMan.new = function (name) local self = People.new(name) -- Append all the key-value pairs in man to the instance in people. copy(self,Man) return selfend Man.sayHello = function () print("Man say hello")end -- Override the sayhi method in the parent class peopleMan.sayHi = function (self) print("Man sayHi "..self.name)end -- Create an instance of manlocal m = Man.new("Lisi")m:sayHi() |
The above code has been annotated in detail, and readers need to carefully read the example given here.
2. Implement object-oriented functions in the form of function closures
| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
-- Implement object-oriented functions in the form of function closures -- Define a method. Function closures implement the concept of a class.function People(name) local self = {} -- Initialization method, private local function init() self.name = name end self.sayHi = function () print("Hello "..self.name) end -- Call Initialization init() return selfend -- Instantiate an objectlocal p = People("ZhangSan")p:sayHi() -- Implement class inheritance in the form of function closuresfunction Man(name) local self = People(name) -- local function init()-- -- end self.sayHello = function () print("Hi "..self.name) end return selfendlocal m = Man("Lisi")--m:sayHello()m:sayHi() |
The above two methods can achieve object-oriented, the second method of visual testing is more concise, specific use according to personal preferences.
Http://cn.cocos2d-x.org/tutorial/show? Id = 1133