Page 258 - Introduction to Programming with Java: A Problem Solving Approach
P. 258

                224 Chapter 6 Object-Oriented Programming
  public class Mouse3Driver
{
public static void main(String[] args)
{
...
minnie.grow(days);
...
} // end main
} // end class Mouse3Driver
 public class Mouse3
{
}
...
public void grow(int days)
{
...
} // end grow
// end class Mouse3
   Figure 6.15 Pass-by-value means a copy of the argument’s value goes to the corresponding parameter Apago PDF Enhancer
6.12 Specialized Methods—Accessors, Mutators, Boolean Methods Let’s now discuss some of the common types of specialized methods. You won’t be asked to learn any new
syntax; you’ll just be asked to apply what you’ve learned so far.
Accessor Methods
An accessor is a method that retrieves part of an object’s stored data—typically private data. Note the following getAge and getWeight methods (taken from Figure 6.14’s Mouse2 class). They are accessor methods as they retrieve the values of the instance variables, age and weight, respectively.
public int getAge()
 {
return this.age;
} // end getAge
public double getWeight()
{
return this.weight;
} // end getWeight
As evidenced by the getAge and getWeight methods, accessor methods should be named with a “get” prefix. That’s why accessor methods are often called get methods.


































































   256   257   258   259   260