
π₯Classes
This page will go over Noir's class system. Classes are used throughout Noir, like for services, etc.
What Are Classes?
---@class Person: NoirClass <-- For intellisense
---@field New fun(self: Person, name: string, occupation: string): Person <-- so the Lua extension thinks Person:New() returns a Person and not a NoirClass
local Person = Noir.Class("Person")
---@param name string
---@param occupation string
function Person:Init(name, occupation)
-- The name of this person
self.name = name
-- The occupation of this person
self.occupation = occupation
-- The comments within this function get picked up by intellisense
-- and are used as descriptions for the attributes above.
-- You can use `---@field` instead though.
end
function Person:PrintInfo()
print(("I am %s, my occupation is %s."):format(self.name, self.occupation))
end
-- Creating an object/instance from the class above
local JohnDoe = Person:New("John Doe", "Unoccupied")
JohnDoe:PrintInfo() -- "I am John doe, my occupation is Unoccupied."
local Bob = Person:New("Bob", "Unoccupied")
Bob:PrintInfo() -- "I am Bob, my occupation is Unoccupied."Creating A Class
Adding Custom Methods
Creating A Class Object From A Class
Inheritance
Intellisense
Final Note
Last updated