功能优势:1.事件使用2级标签【2个key值】,相比起单个key值,2级标签能够适应多种场合与情况,。2.监听的绑定已支持自动销毁,随着物体被销毁而注销事件,并不需要人为的操作,以减少bug。3.事件支持休眠与唤醒功能,在物体隐藏时不在响应事件的接收,唤醒后自动刷新数据。【该功能很好适用于MVC框架,主要给在UI界面使用,界面某个物体隐藏时理应不在刷新数据,而它显示出来时会强制更新一次数据】
PS:该文章部分源码依据于此在lua里实现类似unity生命周期的监听事件_Le_Sam的博客-CSDN博客
正文:
源码
local class3 = function(_name, _super)
local name = _name
local super = _super
if type(_name) ~= "string" then
name = nil
super = nil
end
local class_type = {}
class_type.ctor = false
class_type.super = super
local vtbl = {super = super, className = name}
class_type.vtbl = vtbl
setmetatable(class_type, {
__index = function( t, k )
return vtbl[k]
end,
__newindex = function(t, k, v)
vtbl[k] = v
end
})
if super then
setmetatable(vtbl, {__index =
function(t,k)
local ret = super.vtbl[k]
vtbl[k] = ret
return ret
end
})
end
class_type.new = function(...)
local obj = {}
setmetatable(obj, {__index = vtbl})
do
--[[
local create
create = function(c, ...)
if c.super then
create(c.super, ...)
end
if c.ctor then
c.ctor(obj, ...)
end
end
create(class_type, ...)
]]
if (class_type.ctor) then
class_type.ctor(obj, ...);
end