Lua cocos2d-x
Object-oriented in Cocos2d-x scripting language Lua
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.
-- Object-oriented in Lua -- [copying a table as an object-oriented 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) doins [Key] = varendreturn insend -- [[copying a table. The first parameter is the target table, the second parameter is that the table to be copied is assigned to the target table by traversing the tab.] function copy (Dist, Tab) for key, VAR in pairs (Tab) dodist [Key] = varendend -- defines an empty table, which is equivalent to a class people ={} -- function people. sayhi () -- print ("People say hi") -- end -- defines the method sayhi in the class, and passes in a self parameter people. sayhi = function (Self) print ("People say hi :".. self. name) end -- defines a new method and transmits it to a name parameter. By cloning the people table, an object is generated. It is equivalent to a constructor people in the class. new = function (name) Local Self = clone (people) self. name = namereturn selfend -- local p = clone (people) -- p. sayhi () -- generate a new object local p = people. new ("zhangsan") -- p. sayhi (p) -- P: sayhi () -- defines an empty table and represents a constructor in man ={} -- man class to inherit man from class in Lua. new = function (name) Local Self = people. new (name) -- append all key-value pairs in man to the instance in people to copy (self, man) return selfendman. sayhello = function () print ("man say hello") end -- override the sayhi Method Man in the parent class people. sayhi = function (Self) print ("Man sayhi ".. self. name) end -- create an instance of man local 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
-- 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 = nameend self. sayhi = function () print ("hello ".. self. name) end -- call initialization Init () return selfend -- instantiate an object local p = people ("zhangsan") P: sayhi () -- function closure form implement class inheritance function man (name) Local Self = people (name) -- local function Init () ---- endself. sayhello = function () print ("hi ".. self. name) endreturn 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.