public boolean equals(Object other)

时间:2012-02-29 20:42:06

标签: java

这是一个分数计划。我有私人的ints num和den,Fraction和FractionInterface - 标准的家庭作业问题。我已经做了很多事情,现在已经在equals方法上停留了几个小时。由于其他是一个对象,我不能把它等同于Fraction。这就是我所拥有的:

public boolean equals(Object other){        
     if (other == this){
         return true;
     } else {
         return false;
     }
}

这会编译,但结果不正确:

1/2 eq 1/2 = true
1/2 eq 1/2 = true
1/2 eq 1/2 = false
1/2 eq 1/2 = false

如果我尝试其他== Fraction,它就不会编译。谢谢你的帮助!

9 个答案:

答案 0 :(得分:2)

您可以测试otherFractionInterface的实例并使用演员:

public boolean equals(Object other){        
     if (other == this){
         return true;
     } else if (other instanceof FractionInterface) {
         FractionInterface fOther = (FractionInterface) other;
         // compare numerator and denominator...
     } else {
         return false;
     }
}

请注意,如果instanceoffalse将为other == null,因此无需单独进行空检查。

答案 1 :(得分:1)

试试这个:

首先,转换other对象

Fraction otherFraction = (Fraction) other;

然后,确定两个部分相等的条件。 这将包括一些涉及分子和分母比较的逻辑(期望getNum()使用getDen()otherFraction

答案 2 :(得分:1)

您应该检查参数是否是您的类的实例,如果不是,则返回false并将其投射到您的班级,并根据您的需要进行比较。这样编写equals()方法很常见:

public boolean equals(Object obj) {
    if (!(obj instanceof Fraction)) {
        return false;
    }
    Fraction that = (Fraction) obj;
    ... // Your algorithm to compare two fractions: this and that.
}

您应该确保用于比较两个分数的算法符合equals() documentation中描述的所有要求。

答案 3 :(得分:0)

您需要测试对象是否具有Fraction类,如果是,则将其强制转换为Fraction:

if (other.getClass() != Fraction.class) {
    return false;
}
Fraction otherFraction = (Fraction) other;
// compare the fields of this and otherFraction

在此之前,请务必测试null。

答案 4 :(得分:0)

您正在比较两个对象引用是指同一个对象。

您需要检查other是否为Fraction类型,然后输入转换为Fraction。然后,您将比较Fraction的两个部分。

答案 5 :(得分:0)

我认为你很接近,但你错过了几个关键概念。这是因为你的原则并不适用于所有情况。例如,使用现有代码尝试此操作......

Fraction a = // this is however you're making a fraction object...
Fraction b = // do EXACT same thing here that you did for a

// And then, this will illustrate what is wrong with your program...
if(a.equals(b)) {
  System.out.println("This won't print");
} else {
  System.out.println("This will print because your method just checks for reference");
}

以下是您需要了解的基础知识:

  1. ==equals
  2. 之间的差异
  3. 比较类型而不是参考或值
  4. 通过将“等于”方法放在适当的位置来避免施法
  5. 首先关闭......

    public boolean equals(Object other){        
         if (other == this){
             return true;
         } else {
             return false;
         }
    }
    

    你错过了Java中“equals”方法的观点。 ==用于比较引用,this.equals(foo)用于放置用于比较本地化对象的逻辑。

    您缺少的另一个概念是应该如何使用instanceof。当你问这个......

      

    如果我尝试其他== Fraction,则无法编译。

    这是因为您希望比较对象的类型。要做到这一点,你只需要......

    if(other instanceOf Fraction) {
      // do stuff...
    }
    

    所有这一切,都有最后一个概念,即将等于定义放在适当的位置。你需要把它放在你的Fraction类中并像这样定义它......

    public boolean equals(Fraction other) {
      // do something like this (you will have to define toDouble)
      if(this == other || this.toDouble() == other.toDouble()) {
        return true;
      }
    
      return false;
    }
    

    这将覆盖默认值...

    public boolean equals(Object other) {/* ... */}
    

    它会非常方便。以下是一些示例代码...

    Fraction fractionA = new Fraction("2/4");
    Fraction fractionB = new Fraction("1/2");
    Fraction fractionC = new Fraction("1/3");
    Object trollObject = new Object();
    
    // And then call random equals objects...
    if(fractionA.equals(fractionB)) {
      // should be true...
    }
    
    if(fractionB.equals(fractionA)) {
      // should be true...
    }
    
    // This avoids having to do any casting because
    // since you've only defined a Fraction.equals(Fraction) method
    // it should instead default to the Object.equals method
    if(trollObject.equals(fractionB)) {
    
    }
    

答案 6 :(得分:0)

这是一个非常标准的模式:

public boolean equals(Object other) {
    if (other == this) return true;
    if (other == null) return false;
    if (other.getClass() != this.getClass()) return false;

    Fraction o = (Fraction) other;
    // now you compare their num, den, and possibly sign
}

如果我们应该使用getClass()instanceof,人们可能会争辩。只有当Fraction被扩展时才重要,它取决于扩展时你想要什么。请记住,equals() a.equals(b)的合同应该与b.equals(a)获得相同的结果,如果两者都不是null,并且子类可能有不同的equals()这可能会违反合同。

答案 7 :(得分:-1)

==运算符比较对象的哈希码。这就是你的方法不正确的原因,你应该这样写:

public boolean equals(Object other){        
     if (other instanceof Fraction){
         return ((Fraction)other).getNum == this.num && ((Fraction)other).getDen == this.den;
     } else {
         return false;
     }
}

答案 8 :(得分:-1)

我希望它会对你有帮助。试试这段代码。

import java.io.*;

class Cast

{

public static void main(String args[]) throws IOException

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

byte a=20;

short s=31468;

int i=12345678;

char c=’c';

float f=3.56f;

//Widening or promotion [java question bank][1]

System.out.println(“a=(short)  “+(short) a);

System.out.println(“a=(int)  “+(int) a);

System.out.println(“a=(long)  “+(long)a);

System.out.println(“a=(float)  “+(float)a);

System.out.println();

System.out.println();

System.out.println(“s=(int) “+(int)s);

System.out.println(“s=(long)  “+(long)s);

System.out.println(“s=(float)  “+(float)s);

System.out.println();

System.out.println();

System.out.println(“i=(long)  “+(long)i);

System.out.println(“i=(float)  “+(float)i);

System.out.println(“i=(double)  “+(double)i);


//Narrowing using [java question bank][2]

System.out.println(“f=(byte)  “+(byte)f);

System.out.println(“f=(short)  “+(short)f);

System.out.println(“f=(char)  “+(char)f);

System.out.println(“f=(long)  “+(long)f);

System.out.println();

System.out.println();

System.out.println(“i=(byte)  “+(byte)i);

System.out.println(“i=(short)  “+(short)i);

System.out.println();

System.out.println();

System.out.println(“s=(byte)  “+(byte)s);


}

}
相关问题