阅读背景:

lua 文件路径 字符串处理 数据结构 正则表达式 Class 程序集

来源:互联网 
for uchar in string.gmatch(str, "[%z
for uchar in string.gmatch(str, "[%z\1-\127\194-\244][\128-\191]*") do tab[#tab+1] = uchar end
local _, count = string.gsub(str, "[^\128-\193]", "")
-- set supers|index|create Constructor
function class( classname,... )
local cls = {__cname = classname}
local supers = {...}
-- set cls super
for _,super in pairs(supers) do
local supertype = type(super)
assert(supertype=="table" or supertype=="function",string.format("%s is invalid class type",classname))

-- function as class constructor
if supertype == "function" then
assert(cls.__create == nil,string.format("%s with more than one creating function"),classname)
cls.__create = super
elseif supertype == "table" then
cls.__supers = cls.__supers or {}
table.insert(cls.__supers,super)
if not cls.super then
cls.super = super
end
else
error(string.format("%s is invalid class type",classname))
end
end

-- set cls index
cls.__index = cls
if not cls.__supers or #cls.__supers <= 1 then
setmetatable(cls,{__index = cls.super})
else
setmetatable(cls,{__index = function ( _,key )
for _,super in pairs(cls.__supers) do
if super[key] then
return super[key]
end
end
end})
end

-- set cls create constructor
if not cls.ctor then
cls.ctor = function () end
end
cls.new = function (...)
local instance = nil
if cls.__create then
instance = cls.__create(...)
else
instance = {}
end
setmetatable(instance,cls)
instance.class = cls
instance:ctor(...)
return instance
end
cls.create = function (...)
return cls.new(...)
end

return cls
end


-74-4][8-1]*") do tab[#tab+1] = uchar endfor uchar in string.gmatch(str, "[%z
for uchar in string.gmatch(str, "[%z\1-\127\194-\244][\128-\191]*") do tab[#tab+1] = uchar end
local _, count = string.gsub(str, "[^\128-\193]", "")
-- set supers|index|create Constructor
function class( classname,... )
local cls = {__cname = classname}
local supers = {...}
-- set cls super
for _,super in pairs(supers) do
local supertype = type(super)
assert(supertype=="table" or supertype=="function",string.format("%s is invalid class type",classname))

-- function as class constructor
if supertype == "function" then
assert(cls.__create == nil,string.format("%s with more than one creating function"),classname)
cls.__create = super
elseif supertype == "table" then
cls.__supers = cls.__supers or {}
table.insert(cls.__supers,super)
if not cls.super then
cls.super = super
end
else
error(string.format("%s is invalid class type",classname))
end
end

-- set cls index
cls.__index = cls
if not cls.__supers or #cls.__supers <= 1 then
setmetatable(cls,{__index = cls.super})
else
setmetatable(cls,{__index = function ( _,key )
for _,super in pairs(cls.__supers) do
if super[key] then
return super[key]
end
end
end})
end

-- set cls create constructor
if not cls.ctor then
cls.ctor = function () end
end
cls.new = function (...)
local instance = nil
if cls.__create then
instance = cls.__create(...)
else
instance = {}
end
setmetatable(instance,cls)
instance.class = cls
instance:ctor(...)
return instance
end
cls.create = function (...)
return cls.new(...)
end

return cls
end


-74-



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: