Skip to content

Instantly share code, notes, and snippets.

@svermeulen
Created July 26, 2017 19:18
Show Gist options
  • Save svermeulen/5a2cf4f4016f9b930688c6bb24c3ca45 to your computer and use it in GitHub Desktop.
Save svermeulen/5a2cf4f4016f9b930688c6bb24c3ca45 to your computer and use it in GitHub Desktop.
Lua OOP Inheritance Demo
Base = {}
function Base:new()
local obj = {foo = 'asdf'}
return setmetatable(obj, { __index = self })
end
function Base:run()
print(self.foo)
end
Derived1 = setmetatable({}, { __index = Base })
function Derived1:new()
local obj = {bar = 'bxzcv'}
return setmetatable(obj, { __index = Base.new(self) })
end
Derived2 = setmetatable({}, { __index = Derived1 })
function Derived2:new()
local obj = {quz = 'bhsdgfa'}
return setmetatable(obj, { __index = Derived1.new(self) })
end
thing = Derived2:new()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment