バージョン 2.X
JavaFX は明らかに部分的にオープン ソースです (スタック オーバーフロー リファレンス)。そのリンクから、バージョン2.Xequals()
のクラスのメソッドへのソース コードを見つけました。Color
/**
* Indicates whether some other object is "equal to" this one.
* @param obj the reference object with which to compare.
* @return {@code true} if this object is equal to the {@code obj} argument; {@code false} otherwise.
*/
@Override public boolean equals(Object obj) {
if (obj == this) return true;
if (obj instanceof Color) {
Color other = (Color) obj;
return red == other.red
&& green == other.green
&& blue == other.blue
&& opacity == other.opacity;
} else return false;
}
明らかに、red
、green
、blue
、およびopacity
は同じである必要があります。
バージョン 1.X
バージョン1.Xの場合、コンパイル済みのクラス ファイルを確認したところ、実装が 2.X の場合と同じであると自信を持って言えます (以下のスニペット)。
@com.sun.javafx.runtime.annotation.Public
public boolean equals(java.lang.Object arg0);
4 invokestatic javafx.lang.Builtins.isSameObject(java.lang.Object, java.lang.Object) : boolean [87]
15 instanceof javafx.scene.paint.Color [80]
18 ifeq 121
22 checkcast javafx.scene.paint.Color [80]
28 invokevirtual javafx.scene.paint.Color.get$red() : float [45]
38 invokevirtual javafx.scene.paint.Color.get$red() : float [45]
50 invokevirtual javafx.scene.paint.Color.get$green() : float [47]
60 invokevirtual javafx.scene.paint.Color.get$green() : float [47]
72 invokevirtual javafx.scene.paint.Color.get$blue() : float [48]
82 invokevirtual javafx.scene.paint.Color.get$blue() : float [48]
94 invokevirtual javafx.scene.paint.Color.get$opacity() : float [49]
104 invokevirtual javafx.scene.paint.Color.get$opacity() : float [49]
equals()
実装は1.X から 2.X まで変更されていません。
あなたの本当の問題
Color1
そしてColor2
実際に typeの場合Color
、それらを type のオブジェクトと比較していますString
:
if(Color1.equals("yellow")) && (Color2.equals("yellow"))
比較はここで失敗します:
if (obj instanceof Color)
したがって、equals()
メソッドは常に false を返します。equals()
type の別のオブジェクトで使用する必要がありますColor
。