--普通类
function Class( super )
local class = {}
class.__index = class
class.super = super
if(super) then
setmetatable(cls,{__index = cls.super})--设置元表
end
function class:luaFunc(methodName,params )
if self[methodName] ~= nil then
self[methodName](self,params)
end
end
function class.new(...)
local instance = {}
setmetatable(instance,{__index = cls}) --设置元表
if cls.super then --构造父类 父类也必须构造出新表
cls.super = cls.super.new(...)
setmetatable(cls,{__index = cls.super})
end
if instance.ctor then
instance:ctor(...)
end
return instance
end
return class
end
--扩展类为支持多重继承
function CreateClass(...)
local cls = {}
local parents = {...}
local function search(k, tables)
for i, v in ipairs(tables) do
if v[k] then
return v[k]
end
end
return nil
end
setmetatable(cls, {__index = function(t, k)
return search(k, parents)
end})
function cls:luaFunc(methodName,params)
if self[methodName] ~= nil then
self[methodName](self,params)
end
end
function cls.new(...)
local instance = {}
setmetatable(instance,{__index = cls})
if instance.ctor then
instance.ctor(...)
end
return instance
end
return cls
end
--例如
--基类1
Entity = Class()
function Entity:ctor( )
end
function Entity:IsEntity( )
print("is entity")
end
基类2
Caster = Class()
function Caster:ctor( )
end
function Caster:IsCaster( )
print("is caster")
end
基类3
Target = Class()
function Target:ctor( )
end
function Target:IsTarget( )
print("is target")
end
--子类
Creature = CreateClass(Entity,Caster,Target)
function Creature:Test()
self:IsCaster()
self:IsTarget()
self:IsEntity()
end