File "src/xpl/classes/base.lua"
Allows for object creation. Returns a single object. Call object.make(tbl) to turn a table into an object. alternatively the object table can be subclassed directly;
myObj = object:subclass({member = 'value'})
Copyright © 2011 Thijs Schreijer
Release: Version 0.1, LuaxPL framework.
Functions
| object.make (obj, ...) | Creates an object from a table, a base class. |
Functions
- object.make (obj, ...)
-
Creates an object from a table, a base class. This only need to be done for the very first base class only, descedants or instances will automatically have the object properties. Adds methods
newandsubclassto the supplied table to instantiate and subclass the created objectclass. Property/fieldsuperis added as a reference to the base class of the created object. Methodinitialize(self)will be called upon instantiation.super:new(o); method to create an instance ofsuperin tableo, whilst retaining the properties inosuper:subclass(o); method to create a new class, which inherits fromsuper
super.method(self, param1, param2)) and NOT method notation (super:dosomething(param1, param2)), because in the latter caseselfwill point tosuperand not the instance called upon.Parameters:
-
obj: table to convert into an object -
...: (not used, only for error checking)
Usage:
local base = require("xpl.classes.base")
 
-- create my table
local myObject = {
data = "hello world",
 
print = function(self)
print(self.data)
end,
 
initialize = function(self)
-- upon initialization just print
self:print()
end
}
 
-- make it a class with single inheritance by subclassing
-- it from the base class. The 'initialize()' method will
-- NOT be called upon subclassing
myObject = base:subclass(myObject)
 
-- instantiate an object from the new class and
-- override field contents. This will call 'initialize()'
-- and print "my world".
local descendant = myObject:new({data = "my world"})
 
-- now override another method
function descendant:print()
-- convert data to uppercase
self.data = string.upper(self.data)
-- call ancestor method through 'super'. NOTE: you
-- must use 'function notation' for the call, 'method
-- notation' will not work.
self.super.print(self)
end
 
-- try the overriden method and print "MY WORLD"
descendant:print()