July 2018
Beginner
202 pages
5h 42m
English
When using single inheritance, any class can inherit functionality from only one super class. To implement single inheritance, create a new class through an existing constructor. This can be a bit confusing, so let's see an example.
Start by creating an Animal class; this will be the base class:
Animal = { sound = ""}Animal.new = function(self, object) object = object or {} setmetatable(object, self) self.__index = self return objectendAnimal.MakeSound = function(self) print(self.sound)end
Not every animal is going to make the same sound. Create two new classes, dog and cat, that extend Animal:
-- Dog is a class, not an object (instance)Dog = Animal:new()Dog.sound = "woof"-- Cat is a class, not an Object (instance)Cat = ...