Возьмем, к примеру
time1 = 12:34:56
time2 = 12:34:20
Теперь, если вы compare
эти два Time
s, что происходит, является следующее:
public int compare(Time1 time1, Time1 time2)
{
//time1.getHour() returns 12; time2.getHour() returns 12;
int hourCompare = time1.getHour()-time2.getHour(); //hourCompare = 12-12;
if(hourCompare !=0) //hourCompare is 0, if-block is not entered.
return hourCompare;
//time1.getMinute() returns 34; time2.getMinute() returns 34;
int minuteCompare = time1.getMinute()-time2.getMinute(); //minuteCompare = 34-34;
if(minuteCompare !=0) //minuteCompare is 0, if-block is not entered.
return minuteCompare;
//time1.getSecond() returns 56; time2.getSecond() returns 20;
int secondCompare = time1.getSecond()-time2.getSecond(); //secondCompare = 56-20
if(secondCompare !=0) //secondCompare is 36, the if-block is entered and the value is returned.
return secondCompare;
return 0;
}
Метод compare
вернулся 36, положительное значение, указывающее, что time1
является после time2
.
В том случае, time1.getSecond()
имеет такое же значение, как time2.getSecond()
, если последний блок-также не будут введены и метод, наконец, возвращает 0, что означает, что оба Time
с равны.
Пройдите по этой ссылке, вернитесь, если у вас все еще есть сомнения. Http: //www.tutorialspoint.com/java/java_using_comparator.htm. Я не уменьшал ни одного ;-) – BatScream