Multi-Level Inheritance
Support deep inheritance chains with proper __index delegation.
Single Inheritance Review
Single inheritance is achieved by setting Child.__index = Parent. Method lookup: if not in Child, look in Parent via __index.
Extending Class Factory for Inheritance
Add an :extend() method to the base class that creates a subclass linked to the parent.
function Animal:extend()
local cls = {}
cls.__index = cls
setmetatable(cls, {__index = self})
cls.super = self
function cls:new(...)
local inst = setmetatable({}, cls)
if inst.init then inst:init(...) end
return inst
end
return cls
end