Page 362 - Introduction to Programming with Java: A Problem Solving Approach
P. 362
328 Chapter 8 Software Engineering
/************************************************************
*
MouseShortcut.java
Dean & Dean
This class illustrates uses and omissions of this.
************************************************************/
*
*
*
public class MouseShortcut
{
private int age;
private double weight;
// age in days
// weight in grams
//********************************************************
public MouseShortcut(int age, double weight)
{
setAge(age);
setWeight(weight);
// end constructor
//********************************************************
}
public void setAge(int a)
{
Apago PDF Enhancer
OK to omit this before instance variable, age, because it’s different from parameter, a.
age = a;
} // end setAge
//********************************************************
public void setWeight(double weight)
{
this.weight = weight;
} // end setWeight
//*********************************************************
public void print()
{
}
// end class MouseShortcut
System.out.println("age = " + age +⎫ ⎬
Not OK to omit this before instance variable, weight, because it’s same as parameter, weight.
OK to omit this before age
and weight instance variables.
", weight = " + weight); ⎭ } // end print
Figure 8.11 MouseShortcut class illustrates the use and omission of this