Person
is looking good so far, but it can use some additional behavior
to make it more interesting. Creating behavior means adding methods. This section
looks more closely at accessor methods— namely, the getters and
setters you've already seen in action. Accessor methods
To encapsulate a class's data from other objects, you declare its variables to beprivate
and then provide accessor methods. As you have seen, a
getter is an accessor method for retrieving the value of an attribute; a setter is
an accessor method for modifying that value. The naming of accessors follows a
strict convention known as the JavaBeans pattern, whereby any attribute
Foo
has a getter called getFoo()
and a setter called
setFoo()
. The JavaBeans pattern is so common that support for it is built into the Eclipse IDE. You've even already seen it in action — when you generated getters and setters for
Person
in the preceding section. Accessors follow these guidelines:
- The attribute itself is always declared with
private
access. - The access specifier for getters and setters is
public
. - Getters don't take any parameters and return a value whose type is the same as the attribute it accesses.
- Settings only take one parameter, of the type of the attribute, and do not return a value.
Declaring accessors
By far the easiest way to declare accessors is to let Eclipse do it for you, as shown in Figure 6. But you should also know how to hand-code a getter-and-setter pair. Suppose you have an attribute,Foo
, whose type
is java.lang.String
. A complete declaration for it (following the
accessor guidelines) would be:
1
2
3
4
5
6
7
| private String foo; public String getFoo() { return foo; } public void setFoo(String value) { foo = value; } |
value
as the parameter value
to the setter. This eye-catcher reminds me that I've hand-coded the setter. Because
I usually allow Eclipse to generate getters and setters for me, when I don't,
there's a good reason. Using value
as the setter's parameter value
reminds me that this setter is special. (Code comments also do that.) Calling methods
Invoking, or calling, methods is easy. You saw in Listing 6 how to invoke the various getters ofPerson
to return their values.
Now, I formalize the mechanics of making method calls. Method invocation with and without parameters
To invoke a method on an object, you need a reference to that object. Method-invocation syntax comprises the object reference, a literal dot, the method name, and any parameters that need to be passed:
1
2
| objectReference.someMethod(); objectReference.someOtherMethod(parameter); |
1
2
| Person p = /*obtain somehow */; p.getName(); |
Name
attribute of Person
):
1
| Person p = new Person("Joe Q Author", 42, 173, 82, "Brown", "MALE"); |
1
| new Person("Joe Q Author", 42, 173, 82, "Brown", "MALE"); |
1
2
3
4
5
6
| new Person("Joe Q Author",// Name 42, // Age 173, // Height in cm 82, // Weight in kg "Brown",// Eye Color "MALE");// Gender |
Nested method invocation
Method invocations can also be nested:
1
2
| Logger l = Logger.getLogger(Person.class.getName()); l.info("Name: " + p.getName()); |
Person.class.getName()
to the
getLogger()
method. Remember that the getLogger()
method call is a static method call, so its syntax differs slightly. (You don't need
a Logger
reference to make the invocation; instead, you use the name of
the class as the left side of the invocation.) That's really all there is to method invocation.
0 comments:
Post a Comment