-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathclass.lua
More file actions
30 lines (29 loc) · 729 Bytes
/
class.lua
File metadata and controls
30 lines (29 loc) · 729 Bytes
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
local type = type
local setmetatable = setmetatable
-- 一个精简版的类实现
function class(cls_name)
local cls = { __name = cls_name }
cls.__index = cls
cls.__call = function (cls, ...)
local call = cls[cls_name]
if call then
return call(cls, ...)
end
return
end
cls.new = function (c, ...)
if cls ~= c then
return print("Please use ':' to create new object :)")
end
local t = {}
local ctor = c.ctor
if type(ctor) ~= 'function' then
print("Can't find ctor to init.")
else
ctor(t, ...)
end
return setmetatable(t, cls)
end
return cls
end
return class