Classes Module

From SolarStrike wiki
Revision as of 18:23, 11 July 2011 by Elverion (talk | contribs) (Reserved names)
Jump to: navigation, search

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


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