based on http://lua-users.org/wiki/InheritanceTutorial
class = require "inheritance"
-
you can call 'class' with a table containing the static members:
local Thing = class { create = function(self, v) self.value = 16 --does not need to return anything end, doSomething = function(self) return "boop "..tostring(self.class) end }
-
or with a function that defines the static members. In this context, you can use
This
to refer to the class. Note that you need ()s if you want to pass a function:local Thing = class(function() --use 'This' to refer to the class in this context function This:create() self.value = 16 end function This:doSomething() return "boop "..tostring(self.class) end end)
-
you can use the + operator:
local NewThing = Thing + { }
-
which is really just shorthand for:
local NewThing = Thing:extend { }
-
you can also use functions instead of tables in those scenarios, but you get the idea.
local nt = NewThing(args)
Don't call Thing:create()
. Thing()
maps to a wrapper function that calls Thing:create()
for you so you don't need to return self
at the end of all your create()
functions.
nt.class
: the class object of the instancent
nt.super
: the parent class objectnt < Thing
orThing > nt
tests whethernt
inherits fromThing
, which is just shorthand fornt:isa(Thing)