Class klass
Minimalistic class system for Lua.
Info:
- License: MIT. See LICENSE.md for details.
- Author: Diego MartÃnez https://github.com/kaeza
Fields
klass.__name | Name of the class. |
klass.__super | Superclass of the instance or class. |
Methods
klass:extend (name) | Extend a class. |
klass:isinstance (c) | Test if this object is an instance of a class. |
klass:init (...) | Instance initializer. |
Metamethods
klass:__call (...) | Construct a new instance. |
klass:__tostring () | Get a string representation of this instance. |
Fields
- klass.__name
-
Name of the class.
- __name string
- klass.__super
-
Superclass of the instance or class.
- __super klass
Methods
- klass:extend (name)
-
Extend a class.
Parameters:
- name string Name of the new class.
Returns:
-
table
New class table.
Usage:
local sub = klass:extend("sub") function sub:init(x, y) -- constructor end function sub:dosomething() -- do something here end
- klass:isinstance (c)
-
Test if this object is an instance of a class.
Parameters:
- c table Class.
Returns:
-
boolean
True if it is an instance, false otherwise.
Usage:
local sub = klass:extend("sub") local klsinst = klass() local subinst = sub() print(subinst:isinstance(klass)) --> true print(subinst:isinstance(sub)) --> true print(klsinst:isinstance(klass)) --> true print(klsinst:isinstance(sub)) --> false
- klass:init (...)
-
Instance initializer.
The default implementation does nothing. It should be overridden in subclasses to do something useful.
Parameters:
- ... Arguments passed to __call.
Metamethods
- klass:__call (...)
-
Construct a new instance.
Parameters:
- ... any Arguments to be passed to init.
Returns:
-
table
New instance.
Usage:
local sub = klass:extend("sub") function sub:init(x) self.x = x end local inst = sub("foobar") print(inst.x) --> foobar
- klass:__tostring ()
-
Get a string representation of this instance.
The default implementation returns "
". It should be overridden in subclasses if more information is desired. Returns:
-
string
String representation.