Created
July 26, 2017 19:18
-
-
Save svermeulen/5a2cf4f4016f9b930688c6bb24c3ca45 to your computer and use it in GitHub Desktop.
Lua OOP Inheritance Demo
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
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