Effective Java


Use enums any time you need a set of con-



Download 2,19 Mb.
Pdf ko'rish
bet152/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   148   149   150   151   152   153   154   155   ...   341
Bog'liq
Effective Java

Use enums any time you need a set of con-
stants whose members are known at compile time.
Of course, this includes
“natural enumerated types,” such as the planets, the days of the week, and the
chess pieces. But it also includes other sets for which you know all the possible
values at compile time, such as choices on a menu, operation codes, and command
line flags. 
It is not necessary that the set of constants in an enum type stay
fixed for all time.
The enum feature was specifically designed to allow for binary
compatible evolution of enum types. 
In summary, the advantages of enum types over 
int
constants are compelling.
Enums are more readable, safer, and more powerful. Many enums require no
explicit constructors or members, but others benefit from associating data with
each constant and providing methods whose behavior is affected by this data.
Fewer enums benefit from associating multiple behaviors with a single method. In
this relatively rare case, prefer constant-specific methods to enums that switch on
their own values. Consider the strategy enum pattern if some, but not all, enum
constants share common behaviors.


CHAPTER 6
ENUMS AND ANNOTATIONS
168
Item 35: Use instance fields instead of ordinals
Many enums are naturally associated with a single 
int
value. All enums have an
ordinal
method, which returns the numerical position of each enum constant in
its type. You may be tempted to derive an associated 
int
value from the ordinal:
// Abuse of ordinal to derive an associated value - DON'T DO THIS
public enum Ensemble {
SOLO, DUET, TRIO, QUARTET, QUINTET,
SEXTET, SEPTET, OCTET, NONET, DECTET;
public int numberOfMusicians() {

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   148   149   150   151   152   153   154   155   ...   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