Your original code with a few print statements
Lua Code:
Slot = {}
print("Slot =", Slot)
Slot.__index = Slot
setmetatable(Slot, {
__index = Container, -- makes the inheritance work
__call = function (cls, ...)
local self = setmetatable({}, cls)
self:_init(...)
return self
end,
})
-- This is the _init function.
function Slot:_init( bagNumber, slotIndex )
--Container._init(self) -- call the base class constructor
print("self =", self)
self.is_a = "Slot" -- in the parent class, this is set to "Virtual Container"
end
-- the is_a() method
function Slot:is_a()
return self.is_a
end
local s = Slot(1,1)
print("Finally s =", s)
print("Value of is_a", s.is_a)
At each print (excluding the last) you will notice the the table is the same. That means that during the intitialisation of s (local s = Slot(1,1)) when the code gets too:
you're actually doing
In effect overwriting the original funtion declaration of:
Code:
function Slot:is_a()
return self.is_a
end
and turning it into the string "Slot"
I think you're thinking that s should actually be a new table entry in the Slot table.