##Getters and Setters
I have actually heard about getters and setters in C++. I would love to implement them to my Lua classes.
The code below is in C++.
#include <iostream>
using namespace std; // Let's just say I was coding in CodeBlocks IDE
class myClass(){ // Pretty much a class
private: // How to make values in a class private in Lua?
string name;
public:
// Incapsulation
void setName(x){ // The setter
name = x;
}
void getName(){ // The getter
return example;
}
}
int main(){
myClass class;
class.setName("getters and setters in C++")
cout << class.getName() << endl; // cout is equivalent to: print()
}
Lua Implemention
How can I implement something like that into Lua?
local dog = {}
dog.__index = dog -- Maybe to add getters and setters, I have to create classes differently?
function dog.new(dogBarkSound,dogName)
-- The dog's class's properties are accessible using implicit self parameter in methods
return setmetatable(
-- I don't think there is a way to set something private unless using scope.
{dogBark = dogBarkSound,name = dogName}, dog
)
end
function dog:barkSound()
print(self.dogBark) -- Easily accessible by using self
end
Thank you for helping me in advance!
