在lua中创建类和对象
2013-10-24 00:37
393 查看
Account = {balance = 10} function Account:withdraw (v) self.balance = self.balance - v end function Account:deposit (v) self.balance = self.balance + v end function Account:create() local o = {} setmetatable(o, self) self.__index = self return o end a = Account:create() --重写基类方法 function a:deposit(v) print("hello world") end print(a.balance) a:deposit(100)
lua中没有类,但通过setmetatable和__index可以仿制一个类,a就从Account继承了所有的方法和变量,还可以重写方法
相关文章推荐
- Cocos2d-x教程 Lua脚本(3) Lua中如何创建"类"与"对象"
- Lua_ULua创建基本的游戏对象
- Lua复制方式创建对象表。
- 【COCOS2DX-LUA 脚本开发之五】Lua 使用OOP(面对对象思想编程),免Binding创建自定义lua类
- 【COCOS2DX-LUA 脚本开发之五】Lua 使用OOP(面对对象思想编程),免Binding创建自定义lua类
- ulua里如何将C#创建的UnityEngine对象返回给lua
- 【COCOS2DX-LUA 脚本开发之五】Lua 使用OOP(面对对象思想编程),免Binding创建自定义lua类
- 【COCOS2DX-LUA 脚本开发之五】Lua 使用OOP(面对对象思想编程),免Binding创建自定义lua类
- Lua 使用OOP(面对对象思想编程),免Binding创建自定义lua类
- Lua_ uLua_创建对象_017
- Lua_ uLua_创建对象_017
- 【COCOS2DX-LUA 脚本开发之五】Lua 使用OOP(面对对象思想编程),免Binding创建自定义lua类
- Lua_ULua创建基本的游戏对象
- (原创)cocos2d-x 3.0+ lua 学习和工作(1) : 自定义创建对象方法
- Android应用程序窗口(Activity)的窗口对象(Window)的创建过程分析
- SpringAPI手动创建代理对象——ProxyFactory
- 创建UIView对象
- STL(三)vector复杂对象的创建及遍历
- 用Socket和MSHTML对象模型创建自己的浏览器
- ATL揭秘之“对象创建”篇(转)