local Calc = {name = "Calc"}
-- A plain function in the class table: no self
function Calc.get_min(...)
return math.min(...)
end
-- Called on the class with a colon:
-- self is the class itself
function Calc:class_name()
return self.name
end
local BigCalc = {name = "BigCalc"}
-- Class functions are found through the chain
setmetatable(BigCalc, {__index = Calc})
local result = Calc.get_min(3, 2, 5, 1, 4)
-- result = 1
local name1 = Calc:class_name()
-- name1 = Calc
-- Called on a subclass: self is the subclass
local name2 = BigCalc:class_name()
-- name2 = BigCalc
-- BigCalc has no get_min of its own
local inherited = BigCalc.get_min(7, 6)
-- inherited = 6
print("result = " .. result)
print("name1 = " .. name1)
print("name2 = " .. name2)
print("inherited = " .. inherited)