Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet46/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   42   43   44   45   46   47   48   49   ...   341
Bog'liq
Effective Java

Once you’ve violated the 
equals
 contract,
you simply don’t know how other objects will behave when confronted with
your object.
To eliminate the problem, merely remove the ill-conceived attempt to interop-
erate with 
String
from the 
equals
method. Once you do this, you can refactor the
method into a single return statement:
@Override public boolean equals(Object o) {
return o instanceof CaseInsensitiveString &&
((CaseInsensitiveString) o).s.equalsIgnoreCase(s);
}
Transitivity
—The third requirement of the 
equals
contract says that if one
object is equal to a second and the second object is equal to a third, then the first
object must be equal to the third. Again, it’s not hard to imagine violating this
requirement unintentionally. Consider the case of a subclass that adds a new 
value
component
to its superclass. In other words, the subclass adds a piece of


ITEM 10: OBEY THE GENERAL CONTRACT WHEN OVERRIDING EQUALS
41
information that affects 
equals
comparisons. Let’s start with a simple immutable
two-dimensional integer point class:
public class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override public boolean equals(Object o) {
if (!(o instanceof Point))
return false;
Point p = (Point)o;
return p.x == x && p.y == y;
}
...
// Remainder omitted
}
Suppose you want to extend this class, adding the notion of color to a point:
public class ColorPoint extends Point {
private final Color color;
public ColorPoint(int x, int y, Color color) {
super(x, y);
this.color = color;
}
...
// Remainder omitted
}
How should the 
equals
method look? If you leave it out entirely, the imple-
mentation is inherited from 
Point
and color information is ignored in 
equals
comparisons. While this does not violate the 
equals
contract, it is clearly unac-
ceptable. Suppose you write an 
equals
method that returns 
true
only if its argu-
ment is another color point with the same position and color:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   42   43   44   45   46   47   48   49   ...   341




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish