Lua面向对象 --- 单例

时间:2022-06-01 16:33:34

GameManager.lua:

 --单例模式是利用一个全局表来实现的

 GameManager = {}

 Manager = {__index = GameManager}

 function GameManager:new()
local self = {}
setmetatable(self,Manager)
return self
end function GameManager:ShowName()
print("the is an singleton")
end

Main.lua:

 require "GameManager"

 gm = GameManager:new()

 gm:ShowName()

 --[[
运行结果:
the is an singleton
--]]