所以我正在寫一個學校的程序,它涉及兩個對象的時間(t1和t2)。有幾小時,分鐘,秒。一種方法是比較兩者,看看它們是否相等。 在我的驅動程序文件中,它應該像「t1.equals(t2);」並比較兩者。 在該方法內如何獲得它,以便程序「知道」比較來自t1的變量?從對象抓取變量? - 不知道如何說這
繼承人我現在擁有但是這是在我意識到它不應該是「等於(t1,t2);」但是應該是「t1.equals(t2);」
public boolean equals(Time one, Time two)
{
boolean areEqual=true;
int timeOneSecs=one.getSecs();
int timeOneMins=one.getMins();
int timeOneHrs=one.getHrs();
int timeTwoSecs=two.getSecs();
int timeTwoMins=two.getMins();
int timeTwoHrs=two.getHrs();
if (timeOneSecs!=timeTwoSecs)
{
areEqual=false;
}
if (timeOneMins!=timeTwoMins)
{
areEqual=false;
}
if (timeOneHrs!=timeTwoHrs)
{
areEqual=false;
}
return areEqual;
}
我只是不知道如何讓程序知道這兩次比較,如果第一個是撥打第二? (如果這是有道理的)。
't1.equals(t2)'和't2.equals(t1)'將返回相同的結果。 –
你應該閱讀關於'this'' – user3707125
這聽起來像你想覆蓋等於:http://stackoverflow.com/questions/8180430/how-to-override-equals-method-in-java – Dyrborg