Skip to content

Instantly share code, notes, and snippets.

@henriquegogo
Last active September 5, 2022 13:39
Show Gist options
  • Save henriquegogo/e5f776406ffc6a963d9d to your computer and use it in GitHub Desktop.
Save henriquegogo/e5f776406ffc6a963d9d to your computer and use it in GitHub Desktop.
Simple Lua Class
#!./bin/lua
Person = {} do
setmetatable(Person, { __index = string }) -- Optional. Inheritance
local personMessage;
function Person.new(name, age)
local self = setmetatable({}, { __index = Person })
self.name = name
self.age = age
return self
end
function Person.staticMethod()
print("This is a static method")
end
function Person:increaseAge()
self.age = self.age + 1
end
function Person:sayHappyBirthday()
personMessage = self.name .. " have " .. self.age .. " years old. Happy Birthday!"
self:increaseAge()
return personMessage
end
end
local henrique = Person.new("Henrique Gogó", 29)
local daiane = Person.new("Daiane", 31)
print("About " .. henrique.name)
print(henrique:sayHappyBirthday())
print("About " .. daiane.name)
print(daiane:sayHappyBirthday())
print("Now " .. henrique:sayHappyBirthday())
print("Now " .. daiane:sayHappyBirthday())
Person.staticMethod()
henrique.staticMethod()
print(henrique.lower("This CLASS use the 'STRING' as Inheritance"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment