-- Xingyue contributes with each other ~~~ -- This chapter briefly introduces: single inheritance-I have never used multiple inheritance, mainly because it is difficult to use multiple inheritance in Lua ~~~ Personal experience ~~~ Great sweat ~! -- Example: local base = Class ("base") base. _ Index = base function base: ctor (...) print (self. _ cname) -- output: Class Name. Class ("XXX"), self. _ cname is XXX end function base: Init (...) print ("base: init") end return base -- inherit from local test = Class ("test", require "base") test. _ Index = test return test -- create object createinst ("test") -- output: -- Test -- Base: init -- New () the ctor (...) is automatically called (...) function. If the method test is not found, find the parent class base and find the method ctor (...), call. Self. _ cname is the xxx content of class ("XXX"). Here is test. Why is it not base? Because the object is test, Khan ~ You can study it on your own! -- Similarly, call the init (...) method. We will not describe it here ~~~ -- Add ctor (...) to test (...) method, base unchanged function test: ctor (...) end -- create object createinst ("test") -- output: -- Base: init -- no output this time: test. -- Cause: Test: ctor (...) the method overwrites the ctor (...) of the parent class (...) method. New () calls the ctor of the subclass (...) method. (Pitfall ~~~~) -- How can a subclass call the parent class ?? -- Change the ctor (...) in test (...) method, base unchanged function test: ctor (...) self. super. ctor (self ,...) -- call the method end of the parent class -- Use self in the class. super saves the parent class, so self. super is the parent class, self. super. ctor (self ,...) is to call the ctor (...) of the parent class (...) method. -- Create object createinst ("test") -- output: -- Test -- Base: init -- why self. super. ctor (self ,...) use ". "symbol call method ctor, and pass self. -- Don't worry -- first, let's look at why the "." symbol is used. -- Change the ctor (...) in test (...) method, base unchanged function test: ctor (...) self. super: ctor (...) -- call the method of the parent class. Note: The parameter does not have self end -- create the object createinst ("test") -- output: -- base -- Note: The output has changed here -- base: init -- use the symbol ":", then in the ctor (...) self in is self. super, that is, self is the parent class of test. -- Lua in ". difference between "and": "function A: T (k). _ t = K end a: T (5) -- The passed parameters a, self =. T (A, 5) -- the first parameter is set to self, self = A -- so: Self. super: ctor (...) -- the parameter is self. super, that is, base self. super. ctor (self ,...) -- the parameter is self, that is, test -- (haokeng ~~~~~~~~~~~~~) -- Complete code: local base = Class ("base") base. _ Index = base function base: ctor (...) print (self. _ cname) -- output: Class Name. Class ("XXX"), self. _ cname is XXX end function base: Init (...) print ("base: init") end return base -- inherit from local test = Class ("test", require "base") test. _ Index = test function test: ctor (...) self. super. ctor (self ,...) end function test: Init (...) self. super. init (self ,...) end return test -- time is limited. I hope it will help you ~ -- By the way: coco2d-x inherited layer write local T = Class ("T", function () return CC. layer. create () -- Inherit layer end) -- Lua is also a beginner, so some places may not write well, or there are better ways to implement 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/3984869.html
(Original) cocos2d-x 3.0 + Lua learning and work (2): simple introduction to single inheritance