Определение и инициализация

Классы

-- Definition: a table is the class
local SomeClass = {}
-- A field missing in an object is looked up
-- in the class
SomeClass.__index = SomeClass

function SomeClass:hello()
  return "hello"
end

-- Initialization: the class becomes the
-- metatable of a new table
local some_class = setmetatable({}, SomeClass)

-- The object has no fields, hello is
-- found in the class
local is_empty = next(some_class) == nil
-- is_empty = true
local greeting = some_class:hello()
-- greeting = hello

-- There is no class type: an object is a table
local kind = type(some_class)
-- kind = table

print("is_empty = " .. tostring(is_empty))
print("greeting = " .. greeting)
print("kind = " .. kind)