我的equals方法输出为true,而它应该为false

Hr0*_*419 0 java methods equals

我正在编写代码,并遵循给予我的所有指示.当我运行程序但是相同的方法时,所有的代码和方法看起来都很好!基于这些说明,当测试询问点(a,b)---(c,d)是否等于(e,f)---(g,h)时,我应该得到假,但是我得到了真正.谁能让我知道我做错了什么?先感谢您!

 /**
     * The equals method should return true if the given object is equal to the
     * the Object passed in.  Note that this method receives an Object; there is
     * a particular way that this method should be implemented (see notes from class).
     *
     * Notice that two Segments are equal even if their endpoints are swapped
     *  i.e.: (1, 2)---(3, 4) == (3, 4)---(1, 2)
     */

public boolean equals(Object obj) {
        //if (obj instanceof Segment) {
            //Segment other = (Segment) obj;
            //return p1 == other.getP1() && p2 == other.getP2();
        //}
        //else {

            //throw new IllegalArgumentException("undefined");
        //}

        if(obj == null)
            return false;
        if(this == obj)
            return true;
        if(!(obj instanceof Segment))
            return false;

        else if(obj.getClass() != this.getClass()){
            return false;
        }

        else
        {
            Segment S = (Segment)obj;
            if(this.getP1() == S.getP1() &&
                    this.getP2() == S.getP2());
            return true;


            //else if(obj.getP1() != this.getP1() &&
                //  obj.getP2() != this.getP2());
        //  return false;
        }

    }
Run Code Online (Sandbox Code Playgroud)

khe*_*ood 5

if(this.getP1() == S.getP1() &&
                this.getP2() == S.getP2());
                                          ^
Run Code Online (Sandbox Code Playgroud)

取出这个分号.

然后,如果if不满足该语句,您还需要返回一个值.

编辑

目前您的if陈述没有任何意义.取出分号,以便以下返回语句符合if.然后添加一个return false将在if未满足语句时应用的.

像这样:

if (this.getP1()==S.getP1() && this.getP2()==S.getP2()) {
    return true;
}
return false;
Run Code Online (Sandbox Code Playgroud)

或者,更简单地说:

return (this.getP1()==S.getP1() && this.getP2()==S.getP2());
Run Code Online (Sandbox Code Playgroud)