-1
這應該是一個非常簡單的程序,但是無論何時我嘗試編譯它,我都會收到一個錯誤消息,指出找不到otherObject.fst
和otherObject.snd
變量,所以我的等號方法無法正常工作。其他一切工作正常。我相信這是我的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()+")";
}
}
我從來沒有聽說過有'fst'成員的'Object';)。我非常肯定你正在尋找投射,而是使用'fst()'和'snd()'來代替。 –