-- Xingyue contributes with each other ~~~ -- When Using Lua inheritance, it is very troublesome to manually remember the several layers of inheritance relationships when calling the parent class method. The code is -- Example: 1 -- base class local base = Class ("base") base. _ Index = index function base: ctor (...) print ("base: ctor") print (self. _ cname) end function base: Init (t) prite ("base: init") prite (t )--... end return base -- onebase -- the first level inherits the local onebase = Class ("onebase", require "base") onebase. _ Index = ondebase function onebase: ctor (...) sel F. super. ctor (self ,...) -- call the parent class constructor print ("onebase: ctor") end function onebase: Init (t) self. super. init (t) prite ("onebase: init") prite (t )--... end return onebase -- create createinst ("onebase", 55) -- createinst method has a description in (original) cocos2d-x 3.0 + Lua (1) -- output result: -- base: ctor -- onebase: ctor -- onebase: init -- 55 -- Base: init -- 55 -- the code above shows that there is self in onebase. super. ctor (self ,...) And self. Super. INIT (t) Call the method of the parent class. When it is created, it will automatically call the ctor method, and then automatically call the init method. -- This is an inheritance layer. What if I add a layer? -- Twobase -- the second level inherits the local twobase = Class ("twobase", require "onebase") twobase. _ Index = twobase function twobase: ctor (...) self. super. ctor (self ,...) -- call the parent class constructor print ("twobase: ctor") end function twobase: Init (t) prite ("twobase: init") self. super. init (t) prite (t )--... end return twobase -- create createinst ("twobase", 55) -- createinst method has a description in (original) cocos2d-x 3.0 + Lua (1) -- output result :- -The above class inherits from onebase and creates objects without any output. After tracking, it is found that in onebase, self. Super is onebase itself and enters an endless loop. -- Onebase modify: function onebase: ctor (...) self. super. super. ctor (self ,...) -- call the parent class constructor print ("onebase: ctor") end function onebase: Init (t) self. super. super. init (t) prite ("onebase: init") prite (t )--... end -- two layers of inheritance. The code in onebase is changed to two super, that is, self. super. super. If there is another inheritance layer, what changes should we make ??? -- For example, local threebase = Class ("threebasebase", require "twobase ")... return threebase -- this is onebase and needs to be changed to self. super. super. super, twobase needs to be modified to self. super. super. Continuing to add inheritance makes it difficult to maintain the entire code ----- bug -- 1 -- at this time, onebase has a sub-class outside the forest: othertwobase, local othertwobase, = Class ("threebasebase ", require "onebase ")... return othertwobase -- This subclass has only two inheritance relationships, while threebase has three inheritance relationships. -- If self. Super. Super is used in onebase, othertwobase can be created, but threebase cannot be created. Similarly, if you change to self. Super, othertwobase cannot create ---- bug -- 2 -- how can we solve these two bugs? -- After thinking, I found that there is no common method. As long as the subclass object is passed, I traverse all the methods of the parent class (single inheritance) of the inheritance relationship. Implementation: -- traverse all: constructor ergsuperctor (OBJ ,...) if OBJ then local function again (obj2 ,...) if obj2 and obj2.cotr then again (obj2.super ,...) -- the two orders cannot be reversed. The principle is that the constructor of the parent class must be created first... Obj2.ctor (self ,...) end end again (obj. super ,...) endend -- traverse all: init function ergsuperinit (OBJ ,...) if OBJ then local function again (obj2 ,...) if obj2 and obj2.int then again (obj2.super ,...) -- the two orders cannot be reversed. The principle is that the constructor of the parent class must be created first... Obj2.init (self ,...) end end again (obj. super ,...) endend -- through the above method, you only need to call in the outermost layer, You can traverse all methods of the parent class. -- Note: the parent class does not need to have commands such as self. Super to call the parent class. -- Hope to get help from all the experts -- Question: 1. Is there a way to implement it: using a function as a parameter, there is only one traversal function, and you can freely develop a method to traverse that function? -- Example: -- traverse all: fc function ergsuperfc (OBJ, FC ,...) if OBJ then local function again (obj2 ,...) if obj2 and obj2.fc then again (obj2.super ,...) -- the two orders cannot be reversed. The principle is that the constructor of the parent class must be created first... Obj2.fc (self ,...) end end again (obj. super ,...) end end -- the author has used various methods and has passed in a function as a parameter. I hope you can give me some advice ~~~~ 2. the above two functions have repeated code writing, so I have thought about making a function and calling the corresponding method by passing the type parameter; but I think that generally only these two cases exist, ctor, init needs to traverse all parent classes, and none of them do so to avoid passing Error Parameters during use. But write down your own idea Code, as follows: function ergsuperfc (OBJ, Ty ,...) if OBJ then local function again (obj2 ,...) if obj2 then again (obj2.super ,...) -- the two orders cannot be reversed. The principle is that the constructor of the parent class must be created first... Local FC if Ty = "ctor" then fc = obj2.ctor elseif ty = "init" then fc = obj2.init end FC (self ,...) end end again (obj. super ,...) end end -- finally finish this chapter. I hope it will help you ~ -- Lua I am also a beginner, so some areas may not be well written, or there are better ways to achieve it. I hope you can give me more advice and provide more guidance. xingyue will try to improve it !!! -- Thank you for taking the time to read your work. Thank you ~~~ I wish you all a great deal and everything went well ~~~ For the next chapter, see ~~~~
The author uses cocos2d-x 3.0 + Lua learning and work experience, without the author's consent, please do not reprint! Thank you for your patience ~~~
This article is not approved by the author and cannot be reproduced. Otherwise, relevant responsibilities will be held accountable. For more information, see the source !!~~
Address: http://www.cnblogs.com/wodehao0808/p/3984749.html
(Original) Learning 3: subclass traverses all parent class-specific methods