forked from vimfung/LuaScriptCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.lua
More file actions
131 lines (93 loc) · 2.6 KB
/
class.lua
File metadata and controls
131 lines (93 loc) · 2.6 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
Class = {};
Class.__index = Class
Class.objCount = 0;
Class.name = "Object";
local Class_Constructor = {};
Class_Constructor.__call = function (type)
Class.objCount = Class.objCount + 1;
local instance = {};
instance.class = type;
setmetatable(instance, type.prototype);
return instance;
end
setmetatable(Class, Class_Constructor);
Class.__call = Class_Constructor.__call;
-- function Class:create()
-- Class.objCount = Class.objCount + 1;
-- local instance = {};
-- instance.class = self;
-- setmetatable(instance, self.prototype);
-- return instance;
-- end
function Class:subclass(typeName)
-- 以传入类型名称作为全局变量名称创建table
_G[typeName] = {};
-- 设置元方法__index,并绑定父级类型作为元表
local subtype = _G[typeName];
subtype.name = typeName;
subtype.super = self;
subtype.__call = Class_Constructor.__call;
subtype.__index = subtype;
setmetatable(subtype, self);
-- 创建prototype并绑定父类prototype作为元表
subtype.prototype = {};
subtype.prototype.__index = subtype.prototype;
subtype.prototype.__gc = self.prototype.__gc;
subtype.prototype.__tostring = self.prototype.__tostring;
setmetatable(subtype.prototype, self.prototype);
return subtype;
end
Class.prototype = {};
Class.prototype.__index = Class.prototype;
Class.prototype.__gc = function (instance)
print(instance, "destroy");
end
Class.prototype.tag = 999;
Class.prototype.__tostring = function (instance)
return "[" .. instance.class.name .." object]";
end
function Class.prototype:add(a, b)
-- body
return a + b;
end
Class:subclass("Child");
function Child.prototype:sumSquare(a, b)
-- body
local sum = Child.super.prototype.add(self, a, b);
return sum ^ 2;
end
local c = Child();
print (c:sumSquare(2, 2));
local obj = Class();
print (obj);
-- print (obj.tag);
-- obj.tag = 999;
-- obj = nil;
-- local obj2 = Class:create();
-- print (obj2.tag);
-- obj2 = nil;
-- print (Class.objCount);
Class:subclass("Person")
Person.prototype.name = "vim";
-- function Person.prototype:output(msg)
-- msg = self.name .. ":" .. msg;
-- Person.super.prototype.output(self, msg);
-- -- self.super.output(self, msg);
-- end
local p = Person();
print (p);
print (p.name);
-- p:output("Hello World!");
-- p = nil;
-- Person:subclass("Chinese");
-- Chinese.prototype.skin = "yellow";
-- function Chinese.prototype:output(msg)
-- -- body
-- msg = "(" .. self.skin.. ")" .. msg;
-- Chinese.super.prototype.output(self, msg);
-- end
-- local ch = Chinese:create();
-- print (ch.name, ch.skin);
-- ch:output("Hello World!");
-- ch = nil;
collectgarbage("collect")