Pseudo-class script - could it be done any better?
0 replies, posted
[lua]
-- Need to define a superclass which all other classes are based on.
_CSuperclass = {}
_MT_CSuperclass = {}
-- @FUNC _CSuperClass:New
-- @PARAM ... (variable)
-- @DESC Creates a copy of the 'self' object, which inherits from self (and self's parents).
-- @RETURNS A new class, or an instance of a class.
function _CSuperclass:New(...)
local Output = table.copy(self)
local Param = arg
local meta = table.copy(_MT_CSuperclass)
meta.parent = self
setmetatable(Output, meta)
if self.onInit ~= nil then
Param.n = nil
Output = self.onInit(Output, Param)
end
return Output
end
_MT_CSuperclass.Parent = _CSuperclass
function _MT_CSuperclass:__index(t, k)
local meta = getmetatable(self)
if not t[k] then
return meta.parent[k] or nil
end
end
class = _CSuperclass
[/lua]
Really my first real effort with metatables. Is there any way this could be done better?
Sorry, you need to Log In to post a reply to this thread.