Java Reference
In-Depth Information
public void incrementSeconds()
{
sec++;
//increment the value of sec by 1
if (sec > 59)
//if sec is greater than 59
{
sec = 0;
//set sec to 0
incrementMinutes();
//increment minutes
}
}
From the definitions of the methods incrementMinutes and incrementSeconds , you
can see that a method of a class can call other methods of the class .
The method equals has the following definition:
public boolean equals(Clock otherClock)
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}
Let's see how the method equals works.
Suppose that myClock and yourClock are as shown in Figure 8-9.
hr 12
min 35
sec 48
hr 12
min 27
sec 15
myClock
yourClock
FIGURE 8-9 Objects myClock and yourClock
Consider the following statement:
if (myClock.equals(yourClock))
.
.
.
In the expression:
myClock.equals(yourClock)
myClock accesses the method equals . The value of the parameter yourClock is passed
to the formal parameter otherClock , as shown in Figure 8-10.
 
Search WWH ::




Custom Search