Lua面向对象实现
2016-05-31 20:46
417 查看
function baseClass(base) local cls = {} if base then cls = {} for k,v in pairs(base) do cls[k] = v end cls.base = base else cls = {ctor = function() end} end --cls.__cname = classname cls.__index = cls function cls:new(...) local instance = setmetatable({}, cls) local create create = function(c, ...) if c.base then create(c.base, ...) end if c.ctor then c.ctor(instance, ...) end end create(instance, ...) --instance.class = cls return instance end return cls end
这个类主要是把基类和派生类绑定起来,并且调用ctor构造函数
用法如下
local base = baseClass()
function base:ctor()
end
function base:funcA()
end
function base:funcB(value)
end
local top = baseClass(base)
function top:ctor()
end
function top:funcB(value)
self.base.funcA(self, value)
end
注意调用父类的方法要用"."别用":"是因为baseClass实现问题,不能用语法糖了
相关文章推荐
- Middle-题目102:150. Evaluate Reverse Polish Notation
- C调Lua函数
- Lua中WriteFile和ReadFile
- 关于starrydb的压力测试脚本的使用
- lua学习:lua中“类”的实现
- Lua点号和冒号区别
- lua学习之table类型
- leetcode evaluate-reverse-polish-notation
- Lua使用心得(2)
- Lua使用心得(1)
- 发布starrydb的演示版本
- lua脚本中字符串分割split
- 利用lua中的string.gsub来巧妙实现json中字段的正则替换
- Lua面向对象实现
- Lua内存的监测和回收
- Lua内存泄露情况举例
- 150. Evaluate Reverse Polish Notation
- lua 模式匹配进阶
- LUA模式匹配小试牛刀
- evaluate-reverse-polish-notation(Leetcode)