Created
May 10, 2014 18:32
-
-
Save katlogic/6552ad8cc8087940b736 to your computer and use it in GitHub Desktop.
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
-- class implementation | |
local function class(t) | |
t.__index = t | |
return t | |
end | |
local function extend(dst,src) | |
for k,v in pairs(src) do | |
if type(k) == "string" and k:sub(1,1) ~= "_" then | |
dst[k] = dst[k] or v | |
end | |
end | |
dst.__index = dst | |
return dst | |
end | |
-- example class Foo | |
local Foo = class{ | |
attr1 = 1, | |
attr2 = 2, | |
} | |
function Foo:getattr1() | |
return self.attr1 | |
end | |
function Foo:getattr2() | |
return self.attr1 | |
end | |
-- example class Bar which inherits from Foo | |
local Bar = extend({ | |
attr3 = 3, | |
attr1 = 4, | |
}, Foo) | |
function Bar:getattr3() | |
return self.attr3 | |
end | |
-- usage | |
inst = setmetatable({attr3=1337}, Bar) | |
print(inst:getattr3(3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment