Skip to content

Instantly share code, notes, and snippets.

@josefnpat
Last active March 12, 2016 14:49
Show Gist options
  • Save josefnpat/4178689 to your computer and use it in GitHub Desktop.
Save josefnpat/4178689 to your computer and use it in GitHub Desktop.
Lua 5.1 OOP example
local fooclass = {}
function fooclass:setColor(color)
self.color = color
end
function fooclass:getColor()
return self.color
end
function fooclass.new()
local self = {}
self.color = "Red"
self.setColor = fooclass.setColor
self.getColor = fooclass.getColor
return self
end
return fooclass
foo = require("fooclass")
bar = foo.new()
bar:setColor("Blue")
fun = foo.new()
fun:setColor("Yellow")
zaz = foo.new()
print("BAR",bar:getColor()) -- Blue
print("FUN",fun:getColor()) -- Yellow
print("ZAZ",zaz:getColor()) -- Red
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment