A simple OOP library for Lua. It has inheritance, metamethods (operators), class variables and weak mixin support.
https://github.com/kikito/middleclass
函数功能解析
1. _createClasss(aClass, super)
返回默认的表
local dict = {}
dict.__index = dict
local aClass = { name = name, super = super, static = {},
__instanceDict = dict, __declaredMethods = {},
subclasses = setmetatable({}, {__mode='k'}) }
创建一个table,包含下列属性:
* name 类名
* super 父类的table
* static 静态表
* __instanceDict 存放类定义的属性和函数(包含父类)
* __decaredMethod (当前类声明的方法,不包含父类)
* subclasses 子类表, 使用弱引用
if super then
setmetatable(aClass.static, { __index = function(_,k) return rawget(dict,k) or super.static[k] end })
else
setmetatable(aClass.static, { __index = function(_,k) return rawget(dict,k) end })
end
设置static表的元表为 __index = __instanceDict(或者super.static)
setmetatable(aClass, { __index = aClass.static, __tostring = _tostring,
__call = _call, __newindex = _declareInstanceMethod })
设置类的元表:__index 为 static, __tostring => 默认tostring,
_call =>调用static.new(), __newindex => _declareInstanceMethod
2. _includeMixin(aClass, mixin)
将一张表合并到另一张表中
for name,method in pairs(mixin) do
if name ~= "included" and name ~= "static" then aClass[name] = method end
end
- 除了”included”和”static”,将mixin中的域全部赋值到aClass中(覆盖)
for name,method in pairs(mixin.static or {}) do
aClass.static[name] = method
end
- 将m