What are setters and getters? Why do I need them? What is a good exam开发者_如何学Cple of them in use in an effective way? What is the point of a setter and getter?
Update: Can I get some coding examples please?
A getter is a method that gets the value of a property. A setter is a method that sets the value of a property. There is some contention about their efficacy, but the points are generally:
for completeness of encapsulation
to maintain a consistent interface in case internal details change
More useful is when you need to add some logic around getting or setting, like validating a value before you write it.
A getter/setter is used to hide a private field from the publicity (you can avoid direct access to a field).
The getter allows you to check a provided value before you use it in your internal field. The setter allows you for instance to apply a different format or just to restrict write access (e.g. to derived classes).
A useful application of a getter can be some kind of lazy loading: The backing field (the private field that is hidden by the getter) is initialized to null. When you ask the getter to return the value, it will check for null and load the value with a more time consuming method. This will happen only the first call, later the getter will provide the already loaded value all the time.
Getters & setters separate interface (getter/setter functions) from implementation (how the data is actually stored).
Getters and Setters allow you to control how data members of an object can be accessed or changed.
In contrast, if you expose your data members directly to the user of the object, the user can change them at will, and the object wouldn't even know that they had been changed.
Don't want people to read a data member? Make the data member private, and don't write a getter that gives the value back. Don't want people to modify a data member? Make the data member private, and don't write a setter for it. Want to control the range of allowed values? Put that in the setter.
One question which might pop out of this is if using a method instead of a direct field access might decrease performance.
Answer is not really as compilers optimize code so that if your method is only doing return field;
, where field
is the field in your class that you hide with the setter/getter, it will actually access the field directly. Thus you get in most cases the same performance, at the same time keeping the option of later on change what set/get methods do.
Effective Java Programming of Joshua Block is a great book with tips on how to write good code, and explains why as well. Why using setter/getter is one of the hints.
Note: You might notice that in some books/documentation fields that present a setter/getter instead of being directly accessible are called 'properties' instead of fields. E.g. in C#, you can even specify that a field is a property and you don't need to define set/get anymore (nice feature I think).
public accessors(getter and setter) make sometimes sense. (I'm annoyed that I have not only to document the member variable of a class but also the 2 mostly meaningless accessor methods. )
It usually doesn't help with encapsulation except in cases mentioned by Jason S.
An java example for some char loaded from a database but should be represented as a boolean value
char boolFromDb;
public boolean getBoolFromDb() {
return boolFromDb == 'T';
}
public void setBoolFromDb(boolean newValue) {
boolFromDb = newValue ? 'T' : 'F';
}
精彩评论