Answers for "create class lua"

Lua
0

lua class

-- Classes in lua are just tables.
className = {}

--[[
	className:methodName(parameters) is the intended way to make
	a constructor or a method for the class.
    
    className.methodName(parameters) however,
    is preferred to make static methods.
--]]
function className:new(...)
	self.__index = self
    return setmetatable({...}, self)
end

function className:isInstance(instance)
	return getmetatable(instance) == self
end

c = className:new(1, 2, 3)
print(className:isInstance(c)) -- returns true
Posted by: Guest on February-05-2022
0

lua class example

Car = {}

function Car.new(position, driver, model)
    local newcar = {}

    newcar.Position = position
    newcar.Driver = driver
    newcar.Model = model

    return newcar
end
Posted by: Guest on June-23-2021

Browse Popular Code Answers by Language