-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.lua
36 lines (33 loc) · 880 Bytes
/
class.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
---@class Object
local Object = {};
Object.super = Object;
function Object:ctor()
end
---@param parent T|nil 父类的原型表,如果为 nil 则使用 Object 作为父类
---@generic T : Object
---@return T
function class(parent)
-- 检查传入的 parent 是否为表
if parent and type(parent) ~= "table" then
error("Parent must be a Object")
end
-- 默认使用 Object 作为基类
parent = parent or Object
local obj = {};
setmetatable(obj, {
__index = parent,
__call = function(self, ...)
local instance = {};
setmetatable(instance, {
__index = self
})
-- instance.ctor判空
if instance.ctor then
instance:ctor(...)
end
return instance;
end
})
obj.super = parent;
return obj;
end