|
|
|
|||
|
Accessor MethodsAn object's instance variables are encapsulated within the object, hidden inside, safe from inspection or manipulation by other objects. With certain well-defined exceptions, the object's methods are the only means by which other objects can inspect or alter an object's instance variables. Encapsulation of an object's data protects the object from corruption by other objects and conceals an object's implementation details from outsiders. This encapsulation of data behind an object's methods is one of the cornerstones of object-oriented programming.
Methods used to obtain information about an object are known as
accessor methods.
The
First, Note thatint len = source.length(); reverseIt doesn't care if String maintains its
length attribute as an integer, as a floating point number, or even if String
computes its length on the fly. reverseIt simply relies on the
public interface of the length method, which returns the length
of the String as an integer. That's all reverseIt needs
to know.
Second, The character returned bysource.charAt(i) charAt is then appended to the
StringBuffer dest. Since the loop variable i
begins at the end of source and proceeds backwards over
the string, the characters are appended in reverse order to the StringBuffer,
thereby reversing the string.
More Accessor MethodsIn addition to | |||
|