Created
August 22, 2018 03:46
-
-
Save yukithm/b31b54a6b276e4f5d798ad82f3a7b0e5 to your computer and use it in GitHub Desktop.
[Lua] OOP 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
function NewClass(baseClass) | |
local newClass = {} | |
if baseClass then | |
setmetatable(newClass, {__index = baseClass}) | |
end | |
function newClass:super(...) | |
local newInst = {} | |
if baseClass then | |
newInst = baseClass:new(...) | |
end | |
setmetatable(newInst, {__index = newClass}) | |
return newInst | |
end | |
function newClass:new(...) | |
return self:super(...) | |
end | |
return newClass | |
end | |
local EventFilter = NewClass() | |
function EventFilter:new(filterFunc) | |
local obj = self:super() | |
obj.enabled = false | |
obj.filterFunc = filterFunc | |
return obj | |
end | |
function EventFilter:enable() | |
self.enabled = true | |
end | |
function EventFilter:disable() | |
self.enabled = false | |
end | |
function EventFilter:apply(event) | |
if self.enabled and self.filterFunc then | |
self.filterFunc(event) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment