Skip to content

Instantly share code, notes, and snippets.

@Zbizu
Created June 22, 2021 19:00
Show Gist options
  • Save Zbizu/f15f4c0f27a6c0c2a6cb71a816b875ee to your computer and use it in GitHub Desktop.
Save Zbizu/f15f4c0f27a6c0c2a6cb71a816b875ee to your computer and use it in GitHub Desktop.
lua "class" example
local Class = setmetatable ({ },
{
__call = function (class, ...)
return class.new (...)
end
}
)
Class.__index = Class
Class.__call = function (self)
return Class.new (self)
end
function Class.new (something)
local self = setmetatable ({ }, Class)
self.something = something
return self
end
function Class:setSomething(something)
self.something = something
end
function Class:getSomething()
return self.something
end
obj_1 = Class("obj 1")
obj_2 = Class("obj 2")
obj_1:setSomething("obj 1 modified")
print(obj_1:getSomething(), obj_2:getSomething())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment