2016-02-16 36 views
-1

所以我正在寫一個學校的程序,它涉及兩個對象的時間(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; 
} 

我只是不知道如何讓程序知道這兩次比較,如果第一個是撥打第二? (如果這是有道理的)。

+0

't1.equals(t2)'和't2.equals(t1)'將返回相同的結果。 –

+0

你應該閱讀關於'this'' – user3707125

+1

這聽起來像你想覆蓋等於:http://stackoverflow.com/questions/8180430/how-to-override-equals-method-in-java – Dyrborg

回答

0

您正在呼籲實例的方法,所以你需要該實例的成員比較那些經過論證的:

public boolean equals (Time other) { 
    return getSecs() == other.getSecs() && 
      getMins() == other.getMins() && 
      getHrs() == other.getHrs(); 
} 

注意,雖然上面會的工作,你或許應該遵循Java的標準和覆蓋Object#equals(Object),處理的概念,即一個Time實例可以不等於一個對象,它不是一個Time實例:

@Override 
public boolean equals (Object o) { 
    if (!(o instanceof Time)) { 
     return false; 
    } 

    Time other = (Time)o; 
    return getSecs() == other.getSecs() && 
      getMins() == other.getMins() && 
      getHrs() == other.getHrs(); 
} 
0

任何合理IDE可以自動生成一個重寫equals(Object o)hashCode()給你。

例如,我所做的只是定義了三個int字段,然後讓發電機休息。這使您可以撥打t1.equals(t2),反之亦然。

public class Time { 
    private int hrs, mins, secs; 

    public Time(int hrs, int mins, int secs) { 
     this.hrs = hrs; 
     this.mins = mins; 
     this.secs = secs; 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 

     Time time = (Time) o; 

     if (hrs != time.hrs) return false; 
     if (mins != time.mins) return false; 
     return secs == time.secs; 

    } 

    @Override 
    public int hashCode() { 
     int result = hrs; 
     result = 31 * result + mins; 
     result = 31 * result + secs; 
     return result; 
    } 
} 
0

這基本上是你想要什麼:

public class Main{ 

    public static void main(String []args){ 
     Time t1 = new Time(1,2,3); 
     Time t2 = new Time(1,2,3); 
     Time t3 = new Time(1,2,4); 
     Object t4 = new Object(); 
     System.out.println(t1.equals(t2)); //Writes true 
     System.out.println(t1.equals(t1)); //Writes true 
     System.out.println(t1.equals(t3)); //Writes false 
     System.out.println(t1.equals(t4)); //Writes false 
     System.out.println(t1.equals(null)); //Writes false 
    } 
} 

這是你的時間類會是什麼樣子:

public class Time 
{ 
    private int sec; 
    private int min; 
    private int hour; 

    public Time(int sec, int min, int hour){ 
     this.sec = sec; 
     this.min = min; 
     this.hour = hour; 
    } 

    @Override 
    public boolean equals(Object obj){ 
     //If you compare to null, obviously they are not the same. 
     if (obj == null) return false; 
     //If they point to the same address in the heap they must be the same. 
     if (this == obj) return true; 
     //If you compare it to something that is not a time object, they must be different 
     if (!(obj instanceof Time)) return false; 
     Time t2 = (Time)obj; 
     return this.sec == t2.getSec() && 
       this.min == t2.getMin() && 
       this.hour == t2.getHour(); 
    } 

    public int getSec(){ 
     return this.sec; 
    } 
    public int getMin(){ 
     return this.min; 
    } 
    public int getHour(){ 
     return this.hour; 
    } 
} 

equals方法是從Object繼承中的所有對象Java的。但是,如果你想實現自己的比較,那麼你應該覆蓋從Object繼承的方法,如上所示。

相關問題