Well, if all you need is a simple class, absolutely! The ideal way would be like this (making use of "self"):
functionHuman(name, age)localself= { name = name, age = age }
functionself.speak()print("Hi, I'm "..self.name ..".")
endfunctionself.birthday()self.age =self.age +1print("Yay, "..self.name .." is "..self.age .." years old")
endreturnselfend
The main issue with that approach is that you're creating extra copies of the functions "speak" and "birthday" every time a new object is made, which consumes extra memory. This can be avoided by using metatables:
-- This is our class's wrapper
Human = {}
-- This is our constructor
functionHuman.new(name, age)returnsetmetatable({
name = name,
age = age,
}, { __index = Human })
endfunctionHuman:speak()print("Hi, I'm "..self.name ..".")
endfunctionHuman:birthday()self.age =self.age +1print("Yay, "..self.name .." is "..self.age .." years old")
end
Vasya = Human.new("Vasya", 31)
Vasya:speak()
Vasya:birthday()
Vasya:birthday()
With metatables, you have a table (object) that contains all definitions used by your instances, and a reference to that table is stored on your object (by using the setmetatable function).
You will also notice that in the example above, I have used colons (:) when calling the function, this is a shortcut in Lua that makes the first argument automatically receive a reference to the object that's being used to call (and when used in a function it takes the self variable), otherwise the code would look like this:
functionHuman.birthday(self)self.age =self.age +1print("Yay, "..self.name .." is "..self.age .." years old")
end
Vasya:birthday(Vasya)
The best way to make private methods is to just make the methods local to the file they are defined in.
-- /MyClass.lua
local function private_method(obj)
-- do stuff with obj
end
local class = {}
function class.new(x)
local self = {}
self.x = x
return setmetatable(self, {__index = class})
end
function class:public_method()
private_method(self)
print(self.x)
end
return class
Then you can do, in your main file...
-- /main.lua
local MyClass = require("MyClass")
local obj = MyClass.new(32)
obj:public_method() -- works!
obj:private_method() -- error - attempt to call nil value
Lua doesn't really have the concept of public or private methods, so in order to implement that you'd need to use something like the debug api to determine which file a caller is calling the method from, which is slow and not great.
Instead, just make local functions and multiple files. You can't access local functions of another file (unless you use a lot of debug trickery), so it's "private" enough.
20
u/wolfe_br Feb 07 '24
Well, if all you need is a simple class, absolutely! The ideal way would be like this (making use of "self"):
The main issue with that approach is that you're creating extra copies of the functions "speak" and "birthday" every time a new object is made, which consumes extra memory. This can be avoided by using metatables:
With metatables, you have a table (object) that contains all definitions used by your instances, and a reference to that table is stored on your object (by using the
setmetatablefunction).You will also notice that in the example above, I have used colons (:) when calling the function, this is a shortcut in Lua that makes the first argument automatically receive a reference to the object that's being used to call (and when used in a function it takes the
selfvariable), otherwise the code would look like this:13
u/9551-eletronics Computercraft graphics research Feb 07 '24
I guess that works, please use locals
1
u/Hawk__Echo Feb 13 '24
Hold on, you can declare functions within functions?
1
u/Expensive_Ad_7658 Feb 10 '24
print("OOP")
end