Polymorphisms of OOP tutorial

I am a Chinese, English is so bad. So I have to put my code listed class: function class(super) local cls = {} local superType = type(super) cls.super = super if superType == “function” then cls.super = function(…) cls.corona = super(…) end function cls.new(…) local instance = setmetatable({}, {__index = cls}) instance:ctor(…) return instance end else if super then setmetatable(cls, {__index = super}) else cls = {ctor = function() end} end function cls.new(…) local instance = setmetatable({}, {__index=cls}) instance:ctor(…) return instance end end return cls end This example does not involve SDK Example1: require(“class”) local base = class() function base:ctor(…) print(“base ctor”) end function base:test() print(“base test”) end local sub = class(base) function sub:ctor(…) print(“sub ctor”) self.super:ctor(…) end function sub:test() print(“sub test”) self.super:ctor() end local obj = sub.new(“Icon.png”) obj:test() Please look at here console : sub ctor base ctor sub test base ctor And more relating to the SDK Example2: local myImage = class(function(filename) return display.newImage(filename) end) function myImage:ctor(image) self.super(image) print(“myimage ctor:”, image) end function myImage:removePrint() print(“myImage remove”) end --local obj = myImage.new(“Icon.png”) --obj.corona.x = display.contentCenterX --obj.corona.y = display.contentCenterY local myMoveImage = class(myImage) function myMoveImage:ctor(…) print(“myMoveImage ctor”) self.super:ctor(…) end function myMoveImage:move() self.corona.x = display.contentCenterX self.corona.y = display.contentCenterY end local obj = myMoveImage.new(“Icon.png”) obj:move() timer.performWithDelay(3000, function() obj.corona:removeSelf() obj:removePrint() end, 1)

Hi @195097484, first welcome to Corona!!!   We have a sub-forum for Chinese speakers that may benefit you more if your English is not good.

Next, to get people to help you, you have to post your code in a way that’s readable.  Use the <> button in the bar above to paste your code in, assuming it’s formatted well to begin with.  No one will be able to read your post above.   Also it really helps to ask good questions and provided what you are seeing and what you expect.  I don’t see a question above.

Finally, your forum name is just a number and while that’s technically legal, people will identify with you more if you have a name.

Rob

Hi @195097484, first welcome to Corona!!!   We have a sub-forum for Chinese speakers that may benefit you more if your English is not good.

Next, to get people to help you, you have to post your code in a way that’s readable.  Use the <> button in the bar above to paste your code in, assuming it’s formatted well to begin with.  No one will be able to read your post above.   Also it really helps to ask good questions and provided what you are seeing and what you expect.  I don’t see a question above.

Finally, your forum name is just a number and while that’s technically legal, people will identify with you more if you have a name.

Rob