-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLuaClass.lua
More file actions
54 lines (45 loc) · 1.36 KB
/
LuaClass.lua
File metadata and controls
54 lines (45 loc) · 1.36 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
-- LuaClass.lua
-- 实现Lua中的类概念
function class(baseClass, body)
local ret = body or {}
if (baseClass ~= nil) then
setmetatable(ret, ret)
ret.__index = baseClass
ret.base = baseClass
end
-- 使用__index元方法创建对象
ret.new = function (self, constructionData, originalSubClass)
local obj
if (self.__index ~= nil) then
if (originalSubClass ~= nil) then
obj = self.__index:new(constructionData, originalSubClass)
else
obj = self.__index:new(constructionData, self)
end
else
obj = constructionData or {}
end
setmetatable(obj, obj)
-- 设置读取对象属性/方法时的查找表,也就是类
obj.__index = self
-- 复制操作运算符
if (self.__operators ~= nil) then
for key, value in pairs(self.__operators) do
obj[key] = value
end
end
return obj
end
ret.isInstance = function (self, otherClass)
local cls = self.__index
while cls do
if cls == otherClass then
return true
end
-- 递归查找
cls = cls.base
end
return false
end
return ret
end