Created
June 22, 2021 19:00
-
-
Save Zbizu/f15f4c0f27a6c0c2a6cb71a816b875ee to your computer and use it in GitHub Desktop.
lua "class" example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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