Oh, it DOES!
Class = {}
function Class:new(o, val)
o = o or {}
setmetatable(o, self)
self.__index = self
o.val = val
return o
end
function Class:changeVal(newval)
self.val = newval
end
print("Create 'Obj1' instance with value of 10")
Obj1 = Class:new(nil, 10)
print("Create 'Obj2' instance with value of 12")
Obj2 = Class:new(nil, 12)
print("Obj1.val = " .. Obj1.val)
print("Obj2.val = " .. Obj2.val)
Obj1:changeVal(400)
print("Obj1.val = " .. Obj1.val)
print("Obj2.val = " .. Obj2.val)
Output:
Create 'Obj1' instance with value of 10
Create 'Obj2' instance with value of 12
Obj1.val = 10
Obj2.val = 12
Obj1.val = 400
Obj2.val = 12
So
function Class:changeVal(newval)
is equivalent to
function Class.changeVal(self, newval)
?
I think I kind of understand now. I should be able to get it working with that small modification, thank you!
It’s frustrating the tutorialpoint article doesn’t make this clear. I’d have thought that unique instance properties is a big part of the point of OOP…
2 Likes