Sunday, May 28, 2017

Introduction to Java programming, Part-1 (8)

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;
}
See that the parameter esteem gone to the setter is named uniquely in contrast to on the off chance that it had been Eclipse-created (where the parameter name would be the same as the trait name — for instance, open void setFoo(String foo)). On the uncommon events when I hand-code a setter, I generally utilize an incentive as the name of the parameter incentive to the setter. This eye-catcher — my own tradition, and one that I prescribe to different designers — advises me that I hand-coded the setter. On the off chance that I don't utilize Eclipse to produce getters and setters for me, I have a justifiable reason. Utilizing an incentive as the setter's parameter esteem advises me that this setter is exceptional. (Code remarks can fill a similar need.)

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 Loggerreference 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.

Previous Post
Next Post

post written by:

0 coment�rios: