lua中的私密性和单一方法实现
2015-07-22 16:57
288 查看
--[[ 1、学习在面向对象 私密性 实现的方式 2、单一方法的做法 思想:两个table 一个是用来表示对象的状态 另一个用来表示状态,接口 我们的table是用第二个接口来实现访问的 像其他语言中的单例(私密性) ]] function newAccount( initlizedBanlance) local self = { balance = initlizedBanlance} local show = function( v ) self.balance = self.balance - v end local getBanlance = function() return self.balance end return { show = show, getBanlance = getBanlance } end acc = newAccount(200) print(acc.getBanlance()) acc.show(100) print(acc.getBanlance()) --单一方法演示(set和get方法) function newObject( value ) return function(action ,v) if action == "get" then return value elseif action =="set" then value = v else error("invalid action") end end end d = newObject( 0 ) print(d("get")) d("set",10) --d:set(10) print(d("get"))
相关文章推荐
- lua中的继承和多继承
- lua中的面向对象
- lua中的模块
- lua中的Serialization
- lua中的table
- 一个lua文件如何调用另一个lua文件的变量?
- Lua 语言 15 分钟快速入门(转)
- 【经典BUG】exe与dll同时使用Lua的一个内存崩溃异常
- SLua 中使用 Lua 5.3 的编译工程
- c++ 使用pbc 解决lua 解析protobuf 问题
- lua笔试
- lua迭代器和仿制药for
- 编译安装lua 时 lua.c:67:31: fatal error: readline/readline.h: No such file or directory
- lua的API
- [转]LUA 学习笔记
- LuaBridge
- 将Lua嵌入IOS程序
- LUA 配置 运行 异常的备忘录
- Algorithms—150.Evaluate Reverse Polish Notation
- c++ lua文章list