簡體   English   中英

public boolean equals(Object other)

[英]public boolean equals(Object other)

這是一個分數計划。 我有私人的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,它就不會編譯。 謝謝你的幫助!

您可以測試other是否是FractionInterface的實例並使用強制轉換:

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;
     }
}

請注意,如果other == null ,則instanceof將為false ,因此不需要單獨的空檢查。

試試這個:

首先,投射other對象

Fraction otherFraction = (Fraction) other;

然后,確定兩個分數相等的條件。 這將包括一些邏輯涉及分子和分母的比較(預計使用getNum()getDen()otherFraction

您應該檢查參數是否是您的類的實例,如果不是,則返回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() 文檔中描述的所有要求。

您需要測試對象是否具有Fraction類,如果是,則將其強制轉換為Fraction:

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

在此之前,請確保還測試null。

您正在比較兩個對象引用是指同一個對象。

您將需要檢查otherFraction類型,然后鍵入將其轉換為Fraction 然后,您將比較Fraction的兩個部分。

我認為你很接近,但你錯過了幾個關鍵概念。 這是因為你的原則並不適用於所有情況。 試試這個例子,使用你現有的代碼......

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. 通過將“等於”方法放在適當的位置來避免鑄造

首先......

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)) {

}

這是一個非常標准的模式:

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()是可能會中斷合同。

我希望它對你有幫助。試試這段代碼。

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);


}

}

==運算符比較對象的哈希碼。 這就是你的方法不正確的原因,你應該這樣寫:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM