首页 > 其他 > 详细

Lua面向对象 --- 单例

时间:2018-06-26 13:12:26      阅读:252      评论:0      收藏:0      [点我收藏+]

工程目录结构:

技术分享图片

GameManager.lua:

 1 --单例模式是利用一个全局表来实现的
 2 
 3 GameManager = {}
 4 
 5 Manager = {__index = GameManager}
 6 
 7 function GameManager:new()
 8     local self = {}
 9     setmetatable(self,Manager)
10     return self
11 end
12 
13 function GameManager:ShowName()
14     print("the is an singleton")
15 end

Main.lua:

 1 require "GameManager"
 2 
 3 gm = GameManager:new()
 4 
 5 gm:ShowName()
 6 
 7 --[[
 8 运行结果:
 9 the is an singleton
10 --]]

码云上的相关工程:https://gitee.com/luguoshuai/LearnLua

Lua面向对象 --- 单例

原文:https://www.cnblogs.com/luguoshuai/p/9228580.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!