-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClass.lua
More file actions
65 lines (64 loc) · 2.02 KB
/
Copy pathClass.lua
File metadata and controls
65 lines (64 loc) · 2.02 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
63
64
65
-- 类默认初始化函数 ctor 仅为类初始对象时使用,且创建时必然会被调用,ctor函数可用来在创建对象时构建默认变量
-- 在子类初始化时,可以通过类名前加双下划线"__"显式引用父类对象并调用其中的自定义初始化函数来传递参数
-- 参数 :类名 ,父类列表
function Class(className, ...)
if type(className) ~= "string" then
error("class name must string type");
end
local cls = {__className = className, __isClass = true};
function cls:ctor() -- defaut Init
end
function cls:__ctor() -- On Init
self["__" .. className] = cls;
-- 父类初始化
if cls.__supers ~= nil then
local k,super;
for k,super in pairs(cls.__supers) do
if type(super) == "table" then
super.__ctor(self);
end
end
end
-- 初始化
cls.ctor(self);
end
local supers = {...};
local k,super;
for k,super in ipairs(supers) do
if type(super) == "table" then
-- 附加父列表
if super.__isClass and type(super.__className) == "string" then
cls.__supers = cls.__supers or {};
cls.__supers[super.__className] = super;
if not cls.__super then
cls.__super = super;
end
else
error(string.format("create class %s with invalid super class", className));
end
else
error(string.format("create class %s with invalid super class type %s", className, type(super)));
end
end
cls.__index = cls
if not cls.__supers or #supers == 1 then
setmetatable(cls, {__index = cls.__super});
else
setmetatable(cls, {__index = function (_, key)
local k,super;
for k,super in pairs(cls.__supers) do
if super[key] then
return super[key];
end
end
end});
end
function cls:new(obj)
local instance = obj or {};
setmetatable(instance, {__index = self});
instance.__class = self;
instance:__ctor(); -- 初始化调用
return instance;
end
return cls;
end