Effective Java


// Use of asSubclass to safely cast to a bounded type token



Download 2,19 Mb.
Pdf ko'rish
bet144/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   140   141   142   143   144   145   146   147   ...   341
Bog'liq
Effective Java

// Use of asSubclass to safely cast to a bounded type token
static Annotation getAnnotation(AnnotatedElement element,
String annotationTypeName) {
Class annotationType
= null; // Unbounded type token
try {
annotationType = Class.forName(annotationTypeName);
} catch (Exception ex) {
throw new IllegalArgumentException(ex);
}
return element.getAnnotation(
annotationType.asSubclass(Annotation.class)
);
}
In summary, the normal use of generics, exemplified by the collections APIs,
restricts you to a fixed number of type parameters per container. You can get
around this restriction by placing the type parameter on the key rather than the
container. You can use 
Class
objects as keys for such typesafe heterogeneous
containers. A 
Class
object used in this fashion is called a type token. You can also
use a custom key type. For example, you could have a 
DatabaseRow
type repre-
senting a database row (the container), and a generic type 
Column
as its key.


This page intentionally left blank 


157
C H A P T E R
6
Enums and Annotations
J
AVA
supports two special-purpose families of reference types: a kind of class
called an 
enum type,
and a kind of interface called an 
annotation type
. This chap-
ter discusses best practices for using these type families.
Item 34: Use enums instead of 
int
 constants
An 
enumerated type
is a type whose legal values consist of a fixed set of
constants, such as the seasons of the year, the planets in the solar system, or the
suits in a deck of playing cards. Before enum types were added to the language, a
common pattern for representing enumerated types was to declare a group of
named 
int
constants, one for each member of the type:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   140   141   142   143   144   145   146   147   ...   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