Adding behavior to a Java class
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
The getters and setters that you found in real life toward the finish of the first area are called accessor strategies. (Snappy audit: A getter is a strategy for recovering the estimation of a characteristic; a setter is a technique for altering that esteem.) To embody a class' information from different items, you pronounce its factors to be private and afterward give accessor strategies.
The naming of accessors takes after a strict tradition known as the JavaBeans design. In this example, any trait Foo has a getter called getFoo() and a setter called setFoo(). The JavaBeans example is common to the point that support for it is incorporated with the Eclipse IDE, as you saw when you created getters and setters for Person.
Accessors follow these guidelines:
- The attribute is always declared with
private
access. - The access specifier for getters and setters is
public
. - A getter doesn't take any parameters, and it returns a value whose type is the same as the attribute it accesses.
- Setters take only 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. But you also need to know how to hand-code a getter-and-setter pair.
Suppose I have an attribute,
Foo
, whose type is java.lang.String
. My complete declaration for Foo
(following the accessor guidelines) is:private String foo; public String getFoo() { return foo; } public void setFoo(String value) { foo = value; } |
Calling methods
Invoking — or calling— methods is easy. The
testPerson
method in Listing 6, for example, invokes the various getters of Person
to return their values. Now you'll learn the formal 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
- Any parameters that need to be passed
The syntax for a method invocation without parameters is:
objectReference.someMethod(); |
Here's an example:
Person p = new Person("Joe Q Author", 42, 173, 82, "Brown", "MALE"); p.getName(); |
The syntax for a method invocation with parameters is:
objectReference.someOtherMethod(parameter1, parameter2, . . ., parameterN); |
And here's an example (setting the
Name
attribute of Person
):Person p = new Person("Joe Q Author", 42, 173, 82, "Brown", "MALE"); p.setName("Jane Q Author"); |
Remember that constructors are methods, too. And you can separate the parameters with spaces and newlines. The Java compiler doesn't care. These next two method invocations are equivalent:
new Person("Joe Q Author", 42, 173, 82, "Brown", "MALE"); |
new Person("Joe Q Author",// Name 42, // Age 173, // Height in cm 82, // Weight in kg "Brown",// Eye Color "MALE");// Gender |
Notice how the comments in the second constructor invocation make it more readable for the next person who might work with this code. At a glance, that developer can tell what each parameter is for.
Nested method invocation
Method invocations can also be nested:
Logger l = Logger.getLogger(Person.class.getName()); l.info("Name: " + p.getName()); |
Here you pass the return value of
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 all there is to method invocation.
0 coment�rios:
Post a Comment