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]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章