Jump to content

Module:Inheritance/doc

From Wikiversity

This is the documentation page for Module:Inheritance

Inheritance

[edit source]

This module contains a basic class together with an extend statement to inherit from existing classes. Thus it is intended for other Lua scripts and not to be invoked by wiki pages.

Please note that inheritance does only work with the basic class introduced and its subclasses.

Basic class with a constructor and (class) type checking. With the extend statement below you can inherit from this class and its subclasses.

You can create an instance of this class via:

local instance = Class();

Although this is possible at the moment consider the basic class to be abstract and instantiate inherited classes only.

Class:init

[edit source]

(Empty) Class constructor that gets called on instantiation by default. Overwrite this method to implement a custom constructor. Since it is empty there is no reason to call this basic constructor before if you do so.

Class:isa

[edit source]

Checks if this object is an instance of a specific class.

Parameters:

  • [Class] aClass: class the object is expected to be of

Returns: Returns true if the object is an instance of the class specified or any class derived from, returns false otherwise.

Class:super

[edit source]

Getter for super class this class was derived from.

Returns: Super class if the class was derived from any class, nil otherwise.

extend

[edit source]

The extend statement allows to inherit from a class. It creates a basic class extending the class specified. Please note that inheritance does only work with the basic class introduced and its subclasses.

Example:

Statement usage

    local i = require("inheritance");
    local Person = i.extend(i.Class);
    --
    function Person:init(name)
        self.name = name;
    end
    --
    function Person:getName()
        return self.name;
    end

Class usage

    local user = Person("Testy");
    return user:getName();

Parameters:

  • [Class] baseClass: class to be extended - must be Class or any class derived from Class

Returns: Returns a basic class extending the class specified. The new class inherits all methods and fields of the base class.