Effective Java


// Wildcard type for a parameter that serves as an E producer



Download 2,19 Mb.
Pdf ko'rish
bet129/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   125   126   127   128   129   130   131   132   ...   341
Bog'liq
Effective Java

// Wildcard type for a parameter that serves as an E producer
public void pushAll(
Iterable
src) {
for (E e : src)
push(e);
}
With this change, not only does 
Stack
compile cleanly, but so does the client code
that wouldn’t compile with the original 
pushAll
declaration. Because 
Stack
and
its client compile cleanly, you know that everything is typesafe.
Now suppose you want to write a 
popAll
method to go with 
pushAll
. The
popAll
method pops each element off the stack and adds the elements to the given
collection. Here’s how a first attempt at writing the 
popAll
method might look:
// popAll method without wildcard type - deficient!
public void popAll(Collection dst) {
while (!isEmpty())
dst.add(pop());
}
Again, this compiles cleanly and works fine if the element type of the destination
collection exactly matches that of the stack. But again, it isn’t entirely satisfactory.
Suppose you have a 
Stack
and variable of type 
Object
. If you pop an
element from the stack and store it in the variable, it compiles and runs without
error. So shouldn’t you be able to do this, too?
Stack numberStack = new Stack();
Collection objects = ... ;
numberStack.popAll(objects);
If you try to compile this client code against the version of 
popAll
shown earlier,
you’ll get an error very similar to the one that we got with our first version of
pushAll

Collection
is not a subtype of 
Collection
. Once
again, wildcard types provide a way out. The type of the input parameter to


ITEM 31: USE BOUNDED WILDCARDS TO INCREASE API FLEXIBILITY
141
popAll
should not be “collection of 
E
” but “collection of some supertype of 
E

(where supertype is defined such that 
E
is a supertype of itself [JLS, 4.10]). Again,
there is a wildcard type that means precisely that: 
Collectionsuper
E>
. Let’s
modify 
popAll
to use it:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   125   126   127   128   129   130   131   132   ...   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