提交 09934371 编写于 作者: Emo_Tiny's avatar Emo_Tiny

【Lua框架】lua继承框架提交

上级 9f5317a3
......@@ -20,11 +20,7 @@ local _luaClass =
DumpObject = _DumpObject
}
local _mtBaseClass =
{
__call = function(self,...) return self:New() end,
__tostring = _Tostring
}
--实例化
local function _Instantiate(self, ...)
......@@ -68,9 +64,9 @@ local function _IsA(thing,kind)
return _registry.class[thing].__type
end
elseif _registry.object[thing] then
return _registery.object[thing].__type == kind
return _registry.object[thing].__type == kind
else
return _registery.object[thing].__type
return _registry.object[thing].__type
end
end
......@@ -106,7 +102,137 @@ local function _Extends(self,name ,flags)
return setmetatable(class,self)
end
function _CreateClass(name,flags)
--抽象继承(虚继承),产生的子类不予许被实例化
local function _AbstractExtends(self)
local c = self:_Extends()
_registry.class[c].__abstract = true
return c
end
--终结继承,产生的子类不允许被继承
local function _FinalExtends(self)
local c = self:Extends()
_registry.class[c].__final = true
return c
end
--调用父类的方法
local function _CallSuperClassFunction(self,f,...)
local superClass = getmetatable(self) --获取原表
if not superClass then
return nil
end
local super = superClass
local s = self
if _IsA(self,"object") then
super = _registry.class[superClass.__index].__superClass
s = superClass.__index
end
while super and (s[f] == super[f] or (super.__invoking) ~= nil and super.__invoking[self] ~= nil and super.__invoking[self][super[f]]) do
s = super
super = _registry.class[class].__superClass
end
if not super then
return nil
end
local method = super[f]
if super.__invoking == nil then
super.__invoking = setmetatable({},{__mode = "k"})
end
if super.__invoking[self] == nil then
super.__invoking[self] = {}
end
super.__invoking[self][method] = true
local result = method(self,...)
super.__invoking[self][method] = nil
return result
end
--安全调用方式
local function _CallOverrideFunction(self,f,...)
local success, ret = xpcall(self[f],debug.traceback(),self,...)
if not success then
logerror(string.format("Error occured during trigger function '%s' error msg '%s'", f, ret))
end
return success,ret
end
--获取类型
local function _GetType(self)
if _IsA(self) == "class" then
return self
elseif _IsA(self) == "object" then
return getmetatable(self).__index
else
return nil
end
end
--获取父类
local function _GetSuperClass(self)
local super = nil
if _IsA(self) == "class" then
super = getmetatable(self)
elseif _IsA(self) == "object" then
super = _GetType(self)
super = super.__index
end
return (super ~= _mtBaseClass and super or nil)
end
--获取子类(只有类才能获取子类,对象不能)
local function _GetSubClasses(self)
assert(_IsA(self,"class"),"GetSubClasses() must be called from class")
return _registry.class[self].__subClass or {}
end
--类或对象的字符串化
local function _ToString(self)
local is = _IsA(self)
if is then
local name = nil
if is == "class" then
name = _registry.class[self].__name
elseif is == "object" then
name = _registry.class[_registry.object[self].__index].__name
end
return ("%s: %s"):format(is, name)
else
return tostring(self)
end
end
--对象或者类的类型判断(即判断self 是不是参数class的类型),如果是,返回true,不是返回false,如果没有填class类型,则直接返回self的类型
local function _TypeOf(self,class,shallow)
if class then
assert(_IsA(self,"class"),"When given, Argument must be a class")
local target = self:GetClass()
repeat
if target == class then
return true
end
target = target:GetSuperClass()
until (not target or shallow)
return false
else
return self:GetClass()
end
end
local _mtBaseClass =
{
__call = function(self,...) return self:New() end,
__tostring = _Tostring
}
--内部的创建类的方法
local function _CreateClass(name,flags)
local newClass = {}
newClass.__index = newClass
......@@ -123,11 +249,11 @@ function _CreateClass(name,flags)
newClass.New = _Instantiate
newClass.Extends = _Extends
newClass.AbstractExtends = AbstractExtends
newClass.FinalExtends = FinalExtends
newClass.AbstractExtends = _AbstractExtends
newClass.FinalExtends = _FinalExtends
newClass.__call = _mtBaseClass.__call -- 提供Class()方式创建实例
newClass.super = _CallSuperClassFunction
newClass.SafeOverrideCall = _CallOverridedFunction
newClass.SafeOverrideCall = _CallOverrideFunction
newClass.GetClass = _GetType
newClass.GetSuperClass = _GetSuperClass
newClass.GetSubClasses = _GetSubClasses
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册