+ 1
Could someone explain what a getter and setter does in really simple words?
2 Antworten
+ 6
Its like an engineer, set a value for an instance, or get it. For example (in real life) You make an empty book, you wrote a value: "Hello World" You setted it, now you are reading it, you're simply getting it. Haha get it? No? Set it first! Ba dum tss! :D
+ 5
Getter and setter provide an interface to attributes of object... providing ability to do some treatments when getting or assigning a value: by just defining an attribut to an object, you can set any value to it, even if that will provocate an later error, by using getter and setter function, you let them filter the value and correct it or forgot it silently, preventing an later error. Imagine you have an object with an attribute storing a list index: with a setter, you can verify that the index you will store is valid ( not out of list boundary ).