Modulo:Inheritance

Da Wikiversità, l'apprendimento libero.
Jump to navigation Jump to search

Inheritance[modifica]

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.

Class[modifica]

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[modifica]

(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[modifica]

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[modifica]

Getter for super class this class was derived from.

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

extend[modifica]

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.


local i = {}

local Class = {}
Class.__index = Class;

setmetatable(Class, {
  __call = function (cls, ...)
    local self = setmetatable({}, cls);
    self:init(...);
    return self;
  end,
});

function Class:init()
end

function Class:isa(aClass)
  local crrClass = self;
  while nil ~= crrClass do
    if crrClass == aClass then
      return true;
    end
    crrClass = crrClass:super();
  end
  return false
end

function Class:super()
  return nil;
end

i.Class = Class;

---

function i.extend(baseClass)
  -- limit inheritance to module class tree
  assert(baseClass ~= nil
    and baseClass["isa"] ~= nil
    and baseClass:isa(Class));

  local thisClass = {}
  thisClass.__index = thisClass;
  
  setmetatable(thisClass, {
    __index = baseClass,
    __call = function (cls, ...)
      local self = setmetatable({}, cls)
      self:init(...);
      return self;
    end,
  });
  
  function thisClass:super()
    return baseClass;
  end
  
  return thisClass;
end

return i;