0Pricing
Lua Academy · Lesson

Base Class and Constructor Design

Design a Class() factory that sets up metatables and new() constructors.

Goal: A Reusable Class Factory

We want a Class() function that returns a new class object with proper constructor, method lookup, and OOP semantics.

Class Factory Skeleton

The factory creates a class table with a metatable for method dispatch.

local function Class()
  local cls = {}
  cls.__index = cls
  cls.__name = "AnonymousClass"
  function cls:new(...)
    local instance = setmetatable({}, cls)
    if instance.init then instance:init(...) end
    return instance
  end
  return cls
end

All lessons in this course

  1. Base Class and Constructor Design
  2. Multi-Level Inheritance
  3. Mixin Composition and Interface Checks
  4. instanceof and Type Introspection
← Back to Lua Academy