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.