Skip to content

SuperClass

Jab edited this page Jul 16, 2025 · 3 revisions

Builder API

-- Create the super-class.
local SuperClass = class 'SuperClass' {
    constructor {
        function()
            print('Hello from SuperClass!');
        end
    }
};

-- Create the class.
local SubClass = class 'SubClass' {
    -- Extend the super-class using this API.
    extends(SuperClass),

    constructor {
        function()
            print('Hello from SubClass!');
        end
    }
};

-- Print out the class structs, showing that the sub-class extends the super-class.
print(SuperClass);
print(SubClass);

-- Create an instance of the class.
SubClass.new();

Basic API

local SuperClass = cool.newClass {
    name = 'SuperClass'
};

SuperClass:addConstructor {
    body = function()
        print('Hello from SuperClass!');
    end
};

local SubClass = cool.newClass {
    name = 'SubClass',
    extends = SuperClass,
};

SubClass:addConstructor {
    body = function()
        print('Hello from SubClass!');
    end
};

-- Print out the class structs, showing that the sub-class extends the super-class.
print(SuperClass);
print(SubClass);

-- Create an instance of the class.
SubClass.new();

Result

class examples.SuperClass
class examples.SubClass extends examples.SuperClass
Hello from SuperClass!
Hello from SubClass!

Clone this wiki locally