-- 构造函数沿父类到子类执行
local ctor
ctor = function (cls, ...)
if cls.super ~= nil then
ctor(cls.super, ...)
if cls.ctor ~= nil then
cls:ctor(...)
end
else
if cls.ctor ~= nil then
cls:ctor(...)
end
end
end
-- 全局class方法
class = function (name, super)
local cls = {}
cls.__cname = name
local metaTab = {}
local tp = type(super)
if tp == 'table' or tp == 'function' then
cls.super = super
metaTab.__index = super
else
cls.super = nil
metaTab.__index = metaTab
end
setmetatable(cls, metaTab)
function cls.new(...)
local ins = {}
setmetatable(ins, {__index = cls})
ctor(ins, ...)
return ins
end
return cls
end
-- 使用方法
require('class')
-- 1.普通使用
local testClass1