家庭作业帮助,Java结对类

家庭作业帮助,Java结对类,java,generics,Java,Generics,我有一个家庭作业,用来比较我在类中创建的泛型对,并将其发送给tester类进行比较,它应该返回true。我不知道我做错了什么。我想这可能是我在比较这两个对象时使用的equals方法。当我认为我在正确的轨道上时,我得到了StackOverflow例外。以下是我代码的一部分: import java.util.ArrayList; public class Pair<T1,T2> implements PairInterface<T1,T2> { private T

我有一个家庭作业,用来比较我在类中创建的泛型对,并将其发送给tester类进行比较,它应该返回true。我不知道我做错了什么。我想这可能是我在比较这两个对象时使用的equals方法。当我认为我在正确的轨道上时,我得到了StackOverflow例外。以下是我代码的一部分:

import java.util.ArrayList;

public class Pair<T1,T2> implements PairInterface<T1,T2>
{
    private T1 aFirst;
    private T2 aSecond;
    Pair p1 = new Pair(aFirst,aSecond);

    public Pair(T1 aFirst, T2 aSecond)
    {

            this.aFirst = aFirst;
            this.aSecond = aSecond;

    }

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

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

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

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

    /**
     * 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.
     */
        @Override
    public boolean equals(Object otherObject)
    {

        Pair p2 = (Pair) otherObject;

        if(otherObject == null)
        {
            return false;
        }

        if(getClass() != otherObject.getClass())
        {
            return false;
        }
                if(p1.equals(p2)){
                    return true;
                }else{
                    return false;
                }



        }



    /**
     * 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.
     */
        @Override
    public String toString()
    {
        return new StringBuilder().append('(').append(fst()).append(',').append(snd()).appen         d(')').toString();
    }
}
import java.util.ArrayList;
公共类对实现PairInterface
{
首先是私人T1;
私人T2秒;
对p1=新对(第一对,第二对);
公共对(第一个T1,第二个T2)
{
this.affirst=affirst;
this.aSecond=aSecond;
}
/**
*获取此对的第一个元素。
*@返回此对的第一个元素。
*/
公共T1 fst()
{
首先返回;
}
/**
*获取此对的第二个元素。
*@返回此对中的第二个元素。
*/
公共T2 snd()
{
返回一秒钟;
}
/**
*将第一个元素设置为第一个。
*@param affirst新的第一个元素
*/
公共无效设置FST(T1 aFirst)
{
this.affirst=affirst;
}
/**
*将第二个元素设置为秒。
*@param aSecond新的第二个元素
*/
公共无效设置ND(T2秒)
{
this.aSecond=aSecond;
}
/**
*检查两对是否相等。请注意
*(a,b)等于对(x,y)当且仅当a为
*等于x,b等于y。
*@如果此对等于aPair,则返回true。否则
*返回false。
*/
@凌驾
公共布尔等于(对象其他对象)
{
对p2=(对)其他对象;
if(otherObject==null)
{
返回false;
}
如果(getClass()!=otherObject.getClass())
{
返回false;
}
如果(p1等于(p2)){
返回true;
}否则{
返回false;
}
}
/**
*生成表示此对的字符串。请注意
*表示这对(x,y)的字符串是“(x,y)”。在这里
*除非x或y或两者都包含空格,否则不包含空格
*他们自己。
*@返回表示此对的字符串。
*/
@凌驾
公共字符串toString()
{
返回新的StringBuilder();
}
}

StackOverFlowException
在递归(提示)中抛出(大多数情况下)

看看你的'equals()`方法:你检查q1是否等于(q2)
等于呼叫等于。。。(使用相同的方法检查q2是否等于q1)

要解决这个问题,您必须使用
equals
方法,而不必在其中再次使用“equals”

我们是怎么做到的
您想知道这两个对象中的值是否相等。

您应该重新编写它并检查值是否相等,而不是对象本身,至少不能使用
equals()

首先,为什么要创建类的新实例

Pair p1 = new Pair(aFirst,aSecond);
您不应该这样做,当您的对象将使用构造函数创建时,您的字段将被初始化

第二,在equals方法中有一个递归问题,它在自身内部调用equals

你必须把方法改成这样

Pair p2 = (Pair) otherObject;

      if (otherObject == null) {
         return false;
      }

      if (this == otherObject) {
         return true;
      }

      if (getClass() != otherObject.getClass()) {
         return false;
      }

      return this.aFirst.equals(p2.aFirst)
            && this.aSecond.equals(p2.aSecond);

p1
的作用是什么?这会导致堆栈溢出。无论何时创建新的
它都会创建一个新的
,从而创建一个新的
等。。。