straight to the code snippet :
--A.lua
A = Core.class()
function A:init()
self:foo()
end
function A:foo()
print("x")
end
--B.lua
B = Core.class(A)
function B:init()
end
function B:foo()
print("y")
end
local b = B.new()
The output is "x"
shouldn't it print "y" ?
i thought that it was because of the different self bug mentioned at my older thread here :
http://www.giderosmobile.com/forum/discussion/1677/object-in-super-class039-init-function-is-not-the-same-as-in-the-inherited-class039-init-function-#Item_3 and here :
http://www.giderosmobile.com/forum/discussion/1234/accessing-children-from-parent-class#Item_14 but now it seems it's already got the same self object but still it doesn't call the overridden function
thank you
Comments
Basically the order goes like this.
You initiate B class,
it initiates A class,
it calls foo method of A class, because it's in its constructor
then returns back to B init method,
does everything that needs to be done there
and returns instance.
And this jumping happens only in constructor, so now if you call
What effect are you trying to achieve? Maybe there's another way
so it is really designed to be like that ?
afaik, at least in other true OO language, the foo called in A will call the foo defined in B as the object b is the instance of class B which inherits A
Yes, I get you, in C#, for example, the B:foo method would have been called and if you haven't specifically said to call A:foo method it won't be called.
But since in Lua OOP is not native, but only programmed on top of it, it has it's own restrictions, but also a lot's of other new capabilities and is very, very, veeerrryyy flexible.
all right, thank you for your fast help.
Yes, then i guess Gideros class can make it more OO-like like the above suggested code output? but for now, i think i'll think of a different way to achieve the same effect
Lua is quite fun and flexible once you understand the way it works.
However think of what you are trying to do like an Abstract Class in C++ so your new class will have all of the functions by default, you can override them with new functions, however when you use
This is how it unfolds
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
if you have worked with C++/Java, when you inherit, there is always the super or the parent and if I recollect correct, the super is always called in most of the functions.
If you need that the parent class is not called, do not inherit from it. Unless I am missing something here.
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps