local bindFunc = {
on = function(self, key, luaAction)
self.bindTb[key] = luaAction
end,
off = function(self, key, luaAction)
self.bindTb[key] = nil
end
}
local Notify = function(self, tbl, key, value)
if tbl.bindTb[key] then
tbl.bindTb[key](value)
end
end
local bindClass = function(tb)
local fieldTb = tb
local cacheTbl = {
bindTb = {},
}
local metTbl = {
__index = function(self, key)
return bindFunc[key]
end,
__newindex = function(tb, key, value)
local old = fieldTb[key]
local new = value
if old ~= new then
fieldTb[key] = new
Notify(self, cacheTbl, key, value)
end
end,
__tostring = function()
return "metTbl"
end
}
return setmetatable(cacheTbl, metTbl)
end
local c = bindClass {
value = 100,
}
local hpChange = function(evt)
print("数值改变了" .. evt)
end
c:on("value", hpChange)
c.value = 200
c.value = 200
c.value = 300
c:off("value", hpChange)
c.value = 400