Difference between revisions of "Classes Module"

From SolarStrike wiki
Jump to: navigation, search
(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)
Line 34: Line 34:
 
end
 
end
 
</source>
 
</source>
 +
 +
 +
== 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
 +
 +
_base

Revision as of 04:59, 11 July 2011

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

_base