Lua闭包

Lua中的闭包函数,如果想在外面调用,必须在闭包的母函数执行后才能正常调用,否则改函数引用为空


_G.UIFather = {}

function UIFather:New(prefab)
local t = { ["prefab"] = prefab }
setmetatable(t, self)
self.__index = self
return t
end

function UIFather:RegisterCloseEvent(instance, func, ...)
function UIFather:OnEvent(event,param)

end
end

function UIFather:OnEnable(instance)

end

function UIFather:OnDisable(instance)

end


_G.A = _G.UIFather:New()

print("母函数调用之前" ,A["OnEnable"], A["OnEvent"],UIFather["OnEvent"])

UIFather:RegisterCloseEvent()

print("母函数调用之后" ,A["OnEnable"], A["OnEvent"],UIFather["OnEvent"])


调用结果为:

母函数调用之前 function: 0054CDF0 nil nil
母函数调用之后 function: 0054CDF0 function: 0054CE70 function: 0054CE70
[Finished in 0.1s]

你可能感兴趣的:(Lua)