Pop.Box/class.lua

39 lines
766 B
Lua
Raw Normal View History

2017-08-14 12:48:36 +00:00
local Class
Class = function(name, parent)
2017-08-14 12:48:36 +00:00
local newClass, base
base = {
__index = base,
__class = newClass
}
newClass = setmetable({
__init = function() end,
__base = base,
__name = name
}, {
__call = function(cls, ...)
local self = setmetable({ }, base)
cls.__init(self, ...)
return self
end
})
if parent then
setmetable(base, {
__parent = parent.__base
})
newClass.__parent = parent
newClass.__index = function(cls, name)
local val = rawget(base, name)
if val == nil then
return parent[name]
else
return val
end
end
if parent.__inherited then
parent:__inherited(newClass)
end
end
return newClass, base
2017-08-14 12:48:36 +00:00
end
return Class