Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查对是否相等_Java_Equals - Fatal编程技术网

Java 检查对是否相等

Java 检查对是否相等,java,equals,Java,Equals,这应该是一个非常简单的程序,但是每当我尝试编译它时,我都会得到一个错误,即无法找到otherObject.fst和otherObject.snd变量,因此我的equals方法无法正常工作。其他一切都很好。我相信这是我的setFst和setSnd方法的问题。我尝试了很多变体,但我似乎无法正确表达它。任何帮助都将不胜感激 public class Pair<T1, T2> implements PairInterface<T1, T2> { // TO DO: Ins

这应该是一个非常简单的程序,但是每当我尝试编译它时,我都会得到一个错误,即无法找到
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()+")";
    }
}
公共类对实现PairInterface
{
//要做的事情:实例变量
公共T1优先;
公众T2秒;
公共T1 fst;
公共T2 snd;
公共对(第一个T1,第二个T2)
{
第一个=第一个;
秒=一秒;
}
/**
*获取此对的第一个元素。
*@返回此对的第一个元素。
*/
公共T1 fst()
{
先把这个还给我;
}
/**
*获取此对的第二个元素。
*@返回此对中的第二个元素。
*/
公共T2 snd()
{
把这个还给我;
}
/**
*将第一个元素设置为第一个。
*@param affirst新的第一个元素
*/
公共无效设置FST(T1 aFirst)
{
//做
affirst=fst;
}
/**
*将第二个元素设置为秒。
*@param aSecond新的第二个元素
*/
公共无效设置ND(T2秒)
{
//做
aSecond=snd;
}
/**
*检查两对是否相等。请注意
*(a,b)等于对(x,y)当且仅当a为
*等于x,b等于y。
*@如果此对等于aPair,则返回true。否则
*返回false。
*/
公共布尔等于(对象其他对象)
{
if(otherObject==null)
{
返回false;
}
如果(getClass()!=otherObject.getClass())
{
返回false;
}
if(otherObject.fst.equals(this.fst)和&otherObject.snd.equals(this.snd))
{
返回true;
}
其他的
{
返回false;
}
//做
}
/**
*生成表示此对的字符串。请注意
*表示这对(x,y)的字符串是“(x,y)”。在这里
*除非x或y或两者都包含空格,否则不包含空格
*他们自己。
*@返回表示此对的字符串。
*/
公共字符串toString()
{
//做
返回“(“+first.toString()+”,“+second.toString()+”)”;
}
}

otherObject仅声明为一种对象类型,因此它不具有您创建的任何类的任何属性。它应该与您试图比较它的对象的类型相同。

我从来没有听说过
object
fst
成员;)。我很确定你正在寻找演员,并使用
fst()
snd()
来代替。这就成功了,真不敢相信我错过了几个小时来我一直在挠头的机会。非常感谢。