Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
class inheritance and method overriding — Gideros Forum

class inheritance and method overriding

gmarinovgmarinov Member
edited August 2013 in Code snippets
Really baffled with this one, @atilim, @ar2rsawseen

I have a:
a = Core.class(Sprite)
function a:init() 
  print ("init a")
end
 
b = Core.class(a)
function b:init()
  print("init b")
end
 
local aa = a.new()  -- should print "init a"
local bb = b.new()  -- should print "init b"?
produces:
init a
init a
init b

I expect the "local b=b.new()" line to only print "init b".
I'm seeing Gideros merge the two methods, so both a.init and b.init execute.
However, I see "init a" then "init b", e.g. the order makes overriding methods impossible.
I'd expect when overriding a method, to call the parent explicitly. (e.g. b:init() would call a:init() explicitly)

Can someone advise on:
1. why the implicit call to a:init()
2. when doing a b.new(), why is a:init() executed first?
3. is method overriding possible at all?

Likes: devfuji

http://esem.name/sound ♫ sound and music at ShadyLabs (Clouds of Steel, Aftermath Alvin)
+1 -1 (+1 / -0 )Share on Facebook

Comments

  • MellsMells Guru
    edited August 2013
    Hi @gmarinov,
    I expect the "local b=b.new()" line to only print "init b".
    To do that, in Gideros you have to do (can't test right now but should work) :
    a = Core.class(Sprite)
    function a:init() 
     
    end
     
    function a:postInit()
         print ("init a")
    end
     
    b = Core.class(a)
    function b:init()
      print("init b")
    end
     
    local aa = a.new()
    local bb = b.new()
    postInit() [more informations] is probably your answer.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • @Mells
    this does work, thank you very much!
    I've never seen postInit() in the documentation??
    http://esem.name/sound ♫ sound and music at ShadyLabs (Clouds of Steel, Aftermath Alvin)
  • MellsMells Guru
    edited August 2013
    @gmarinov
    Glad it helped :)
    I've never seen postInit() in the documentation??
    I am not sure either... at that time a lot of things were happening/changing through the forums and it was the primary source of informations.

    Probably @ar2rsawseen could give us a directly link to the documentation covering that aspect so the discussion can be the definitive source of informations on that topic.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    But before that @ar2rsawseen needs to add this information to documentation, to share a link to it :-\"
Sign In or Register to comment.