Skip to content
This repository has been archived by the owner on Jan 2, 2021. It is now read-only.

Inheritance

Brad Sharp edited this page Nov 26, 2016 · 3 revisions

Inheritance allows a new class to include all the properties and methods of an existing class. luna offers support for multi-object inheritance which means that a single class can inherit multiple classes.

local animal = class {
    Species = "";
}

local winged = class {
    Fly = function (this)
        print'I can fly!'
    end;
    Wings = 2;
}

local mammal = class (animal) {}
local insect = class (animal) {}
local bird = class (animal, winged) {}

local emu = class (bird) {
    Fly = function (this)
        print("I can't fly, but I have " .. this.Wings .. " wings")
    end;
    Species = "Emu";
}

local bee = class (insect, winged) {Species = "Bee";}
local bat = class (mammal, winged) {Species = "Bat";}
local fox = class (mammal) {Species = "Fox";}

Occasionally you might have a class which inherits the same properties from two other classes. When this happens the order in which you inherited the classes is used to decide where the property will be inherited from.

local animal = class {
    Species = "";
}

local winged = class {
    Fly = function (this)
        print'I can fly!'
    end;
    Wings = 2;
}

local flightless = class {
    Fly = function (this)
        print("I can't fly, but I have " .. this.Wings .. " wings")
    end;
    Wings = 2;
}

local bird = class (animal, winged) {}

local emu = class (flightless, bird) {
    Species = "Emu";
}
Clone this wiki locally