2015-01-15 2 views
-1

Это, как предполагается, очень простая программа, однако всякий раз, когда я пытаюсь ее скомпилировать, возникает ошибка, указывающая, что переменная otherObject.fst и otherObject.snd не найдена, поэтому мой метод equals не работает должным образом. Все остальное работает нормально. Я считаю, что это проблема с моими методами setFst и setSnd. Я пробовал кучу вариаций, но я не могу правильно это сформулировать. Любая помощь приветствуется!Проверка того, являются ли пары равными

public class Pair<T1, T2> implements PairInterface<T1, T2> 
{ 
    // TO DO: Instance Variables 
    public T1 first; 
    public T2 second; 
    public T1 fst; 
    public T2 snd; 

    public Pair(T1 aFirst, T2 aSecond) 
    { 
     first = aFirst; 
     second = aSecond; 
    } 

    /** 
    * Gets the first element of this pair. 
    * @return the first element of this pair. 
    */ 
    public T1 fst() 
    { 
     return this.first; 
    } 

    /** 
    * Gets the second element of this pair. 
    * @return the second element of this pair. 
    */ 
    public T2 snd() 
    { 
     return this.second; 
    } 

    /** 
    * Sets the first element to aFirst. 
    * @param aFirst the new first element 
    */ 
    public void setFst(T1 aFirst) 
    { 
     // TO DO 
     aFirst = fst; 
    } 

    /** 
    * Sets the second element to aSecond. 
    * @param aSecond the new second element 
    */ 
    public void setSnd(T2 aSecond) 
    { 
     // TO DO 
     aSecond = snd; 
    } 

    /** 
    * Checks whether two pairs are equal. Note that the pair 
    * (a,b) is equal to the pair (x,y) if and only if a is 
    * equal to x and b is equal to y. 
    * @return true if this pair is equal to aPair. Otherwise 
    * return false. 
    */ 
    public boolean equals(Object otherObject) 
    { 
     if (otherObject == null) 
     { 
      return false; 
     } 

     if (getClass() != otherObject.getClass()) 
     { 
      return false; 
     } 
     if (otherObject.fst.equals(this.fst) && otherObject.snd.equals(this.snd)) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
     // TO DO 
    } 

    /** 
    * Generates a string representing this pair. Note that 
    * the String representing the pair (x,y) is "(x,y)". There 
    * is no whitespace unless x or y or both contain whitespace 
    * themselves. 
    * @return a string representing this pair. 
    */ 
    public String toString() 
    { 
     // TO DO 
     return "("+first.toString()+","+second.toString()+")"; 
    } 
} 
+0

Я никогда не слышал о 'Объекте', имеющем' fst' член;). Я уверен, что вы ищете бросить, и вместо этого используйте 'fst()' и 'snd()'. –

ответ

2

otherObject объявлен как тип объекта, поэтому он не имеет каких-либо атрибутов любого класса, который вы создали. Он должен быть того же типа, что и объект, с которым вы пытаетесь его сравнить.

+0

Это трюк, не могу поверить, что я пропустил это, что часами почесываю голову. Спасибо! –

Смежные вопросы