Classes Module
From SolarStrike wiki
Revision as of 04:11, 17 November 2008 by 12.201.75.247 (talk) (New page: == class == '''table class()''' '''table class(base, constructor)''' '''table class(base)''' '''table class(constructor)''' Creates a Lua class (table) containing the specified base a...)
class
table class()
table class(base, constructor)
table class(base)
table class(constructor)
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.
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