-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass.lua
More file actions
70 lines (63 loc) · 1.95 KB
/
Class.lua
File metadata and controls
70 lines (63 loc) · 1.95 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
-----------------------------------------------------
---- SETCLASS CLONES THE BASIC OBJECT CLASS TO CREATE NEW CLASSES
-----------------------------------------------------
-- Supports INHERITANCE
--
-- Sam Lie, 17 May 2004
-- Modified Code from Christian Lindig - lindig (at) cs.uni-sb.de
---------------------------------------------------------------
-- EVERYTHING INHERITS FROM THIS BASIC OBJECT CLASS
BaseObject = {
super = nil,
name = "Object",
new =
function(class)
local obj = {class = class}
local meta = {
__index = function(self,key) return class.methods[key] end
}
setmetatable(obj,meta)
return obj
end,
methods = {classname = function(self) return(self.class.name) end},
data = {}
}
function setclass(name, super)
if (super == nil) then
super = BaseObject
end
local class = {
super = super;
name = name;
new =
function(self, ...)
local obj = super.new(self, "___CREATE_ONLY___");
-- check if calling function init
-- pass arguments into init function
if (super.methods.init) then
obj.init_super = super.methods.init
end
if (self.methods.init) then
if (tostring(arg[1]) ~= "___CREATE_ONLY___") then
obj.init = self.methods.init
if obj.init then
obj:init(unpack(arg))
end
end
end
return obj
end,
methods = {}
}
-- if class slot unavailable, check super class
-- if applied to argument, pass it to the class method new
setmetatable(class, {
__index = function(self,key) return self.super[key] end,
__call = function(self,...) return self.new(self,unpack(arg)) end
})
-- if instance method unavailable, check method slot in super class
setmetatable(class.methods, {
__index = function(self,key) return class.super.methods[key] end
})
return class
end