Enhance class definition: add inherit method to support base class inheritance
This commit is contained in:
parent
b32a9d8786
commit
45a226cb7b
13
oop/oop.lua
13
oop/oop.lua
@ -70,6 +70,16 @@ function oop.class(def, base)
|
|||||||
function cls.setattr(target, key, value)
|
function cls.setattr(target, key, value)
|
||||||
return oop.setattr(target, key, value)
|
return oop.setattr(target, key, value)
|
||||||
end
|
end
|
||||||
|
function cls.inherit(parent)
|
||||||
|
cls.__base = parent
|
||||||
|
cls.super = parent
|
||||||
|
local mt = getmetatable(cls) or {}
|
||||||
|
mt.__index = parent
|
||||||
|
mt.__call = mt.__call or function(c, ...)
|
||||||
|
return oop.new(c, ...)
|
||||||
|
end
|
||||||
|
setmetatable(cls, mt)
|
||||||
|
end
|
||||||
|
|
||||||
setmetatable(cls, {
|
setmetatable(cls, {
|
||||||
__index = base,
|
__index = base,
|
||||||
@ -85,6 +95,9 @@ function oop.class(def, base)
|
|||||||
elseif def ~= nil then
|
elseif def ~= nil then
|
||||||
error("class definition must be a function or name string")
|
error("class definition must be a function or name string")
|
||||||
end
|
end
|
||||||
|
if base then
|
||||||
|
cls.inherit(base)
|
||||||
|
end
|
||||||
|
|
||||||
return cls
|
return cls
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user