-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass.lua
More file actions
62 lines (52 loc) · 1.31 KB
/
Copy pathClass.lua
File metadata and controls
62 lines (52 loc) · 1.31 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
---@class Class
---@field new function(...:any[...])
---@field init function(...:any[...])
---@field class function():table
---@field super function():table
---@field instanceOf function(klass:table):boolean
Class = {}
---@param base table
---@return table
local function extend(_, base)
local new_class = {}
local class_mt = { __index = new_class }
if base then
setmetatable(new_class, { __index = base })
end
---@param ... any[...]
---@return table
function new_class:new(...)
local inst = {}
setmetatable(inst, class_mt)
inst:init(...)
return inst
end
---@param ... any[...]
function new_class:init(...)
DUSystem.print("init must be implemented")
end
---@return table
function new_class:class()
return new_class
end
---@return table
function new_class:super()
return base
end
---@param klass table
---@return boolean
function new_class:instanceOf(klass)
local is_a = false
local cur = new_class
while (cur and not is_a) do
if (cur == klass) then
is_a = true
else
cur = cur:super()
end
end
return is_a
end
return new_class
end
setmetatable(Class, { __call = extend })