Skip to content

Instantly share code, notes, and snippets.

@yukithm
Created August 22, 2018 03:46
Show Gist options
  • Save yukithm/b31b54a6b276e4f5d798ad82f3a7b0e5 to your computer and use it in GitHub Desktop.
Save yukithm/b31b54a6b276e4f5d798ad82f3a7b0e5 to your computer and use it in GitHub Desktop.
[Lua] OOP example
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