Last active
March 12, 2016 14:49
-
-
Save josefnpat/4178689 to your computer and use it in GitHub Desktop.
Lua 5.1 OOP example
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
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 |
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
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