Difference between revisions of "Classes Module"
(→Reserved names) |
(→class) |
||
Line 7: | Line 7: | ||
'''table class(constructor)''' | '''table class(constructor)''' | ||
+ | |||
+ | '''table class(base, constructor, inheritctor)''' | ||
Creates a Lua class (table) containing the specified base and/or constructor. 'base' (if applicable) should be another Lua class created with the class() function that you want to inherit from. 'constructor' should be a function to help construct (initialize) this class. | Creates a Lua class (table) containing the specified base and/or constructor. 'base' (if applicable) should be another Lua class created with the class() function that you want to inherit from. 'constructor' should be a function to help construct (initialize) this class. | ||
+ | |||
+ | 'inheritctor' defaults to true. If it is false, the base constructor (and its' parent's constructor, etc.) will not be called, and the new constructor will completely override it. | ||
+ | |||
'''Example''' | '''Example''' | ||
Line 35: | Line 40: | ||
</source> | </source> | ||
+ | |||
+ | You may also use an alternative declaration which looks much nicer and is easier to read. | ||
+ | |||
+ | '''Example''' | ||
+ | <source lang="lua"> | ||
+ | -- Create the base class | ||
+ | baseclass = class(); | ||
+ | function baseclass:constructor() | ||
+ | printf("baseclass constructor called.\n"); | ||
+ | self.number = 1; | ||
+ | self.text = "test"; | ||
+ | end | ||
+ | |||
+ | child1 = class(baseclass) | ||
+ | function child1:constructor() | ||
+ | printf("child constructor called.\n"); | ||
+ | -- self.number is being set by the baseclass's constructor | ||
+ | self.text = "modified by child:constructor()"; | ||
+ | end | ||
+ | |||
+ | child2 = class(baseclass, nil, false) | ||
+ | function child2:constructor() | ||
+ | -- baseclass's constructor will not be called! | ||
+ | -- self.number and self.text will be nil unless | ||
+ | -- we were to modify them here. | ||
+ | end | ||
+ | |||
+ | child3 = class(baseclass) | ||
+ | function child3:constructor(_num) | ||
+ | -- self.number is being set by baseclass, | ||
+ | -- but we can optionally pass in a variable | ||
+ | -- to modify it in the constructor of this new class. | ||
+ | self.number = _num or self.number; | ||
+ | end | ||
+ | |||
+ | |||
+ | a = child1(); --[[ This should output: | ||
+ | baseclass constructor called. | ||
+ | child constructor called. | ||
+ | ]] | ||
+ | |||
+ | b = child2(); -- Doesn't really do anything, since nothing was done in it's constructor | ||
+ | |||
+ | c = child3(); -- c.number is 1, as set in baseclass's constructor | ||
+ | d = child3(2); -- d.number is 2, as set in child3's constructor | ||
+ | </source> | ||
== Reserved names == | == Reserved names == |
Revision as of 18:40, 11 July 2011
class
table class()
table class(base, constructor)
table class(base)
table class(constructor)
table class(base, constructor, inheritctor)
Creates a Lua class (table) containing the specified base and/or constructor. 'base' (if applicable) should be another Lua class created with the class() function that you want to inherit from. 'constructor' should be a function to help construct (initialize) this class.
'inheritctor' defaults to true. If it is false, the base constructor (and its' parent's constructor, etc.) will not be called, and the new constructor will completely override it.
Example
-- Create a base class with constructor
baseclass = class(function (a,number) a.number = number printf("Num set\n"); end);
function baseclass:testing()
printf("This is a test.\n");
end
-- Inherit from it
child1 = class(baseclass);
-- Polymorphism...
child2 = class(baseclass);
function child2:testing()
printf("This is a test (from child2).\n");
end
-- Constructors + Polymorphism (remember, inheriting from baseclass)
child3 = baseclass(120);
function child3:testing()
printf("Child 3\'s number is %d.\n", self.number);
end
You may also use an alternative declaration which looks much nicer and is easier to read.
Example
-- Create the base class
baseclass = class();
function baseclass:constructor()
printf("baseclass constructor called.\n");
self.number = 1;
self.text = "test";
end
child1 = class(baseclass)
function child1:constructor()
printf("child constructor called.\n");
-- self.number is being set by the baseclass's constructor
self.text = "modified by child:constructor()";
end
child2 = class(baseclass, nil, false)
function child2:constructor()
-- baseclass's constructor will not be called!
-- self.number and self.text will be nil unless
-- we were to modify them here.
end
child3 = class(baseclass)
function child3:constructor(_num)
-- self.number is being set by baseclass,
-- but we can optionally pass in a variable
-- to modify it in the constructor of this new class.
self.number = _num or self.number;
end
a = child1(); --[[ This should output:
baseclass constructor called.
child constructor called.
]]
b = child2(); -- Doesn't really do anything, since nothing was done in it's constructor
c = child3(); -- c.number is 1, as set in baseclass's constructor
d = child3(2); -- d.number is 2, as set in child3's constructor
Reserved names
Please be aware that when using classes, there are a few reserved names. You should not create any variables or functions with these names as it may interfere with the functionality of the class.
The following names are reserved:
constructor
parent
is_a