, , . , , , . , , ...
Fraction a =
Fraction b =
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");
}
, , :
...
public boolean equals(Object other){
if (other == this){
return true;
} else {
return false;
}
}
"equals" Java. == , this.equals(foo) .
, , , instanceof. ...
== , .
, . , ...
if(other instanceOf Fraction) {
}
, , . ...
public boolean equals(Fraction other) {
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();
if(fractionA.equals(fractionB)) {
}
if(fractionB.equals(fractionA)) {
}
if(trollObject.equals(fractionB)) {
}