Last active
September 5, 2022 13:39
-
-
Save henriquegogo/e5f776406ffc6a963d9d to your computer and use it in GitHub Desktop.
Simple Lua Class
This file contains hidden or 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
#!./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