— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet531/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   527   528   529   530   531   532   533   534   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

animals[0] = new Cat();
}
We put a new Cat into a Dog array. The 
compiler allowed it, because it knows that 
you might have passed a Cat array or Animal 
array to the method, so to the compiler it 
was possible that this was OK.
%java TestGenerics1
Exception in thread “main” java.lang.ArrayStoreException: 
Cat
at TestGenerics1.takeAnimals(TestGenerics1.java:16)
at TestGenerics1.go(TestGenerics1.java:12)
at TestGenerics1.main(TestGenerics1.java:5)
File Edit Window Help CatsAreSmarter
It compiles, but when we run it:
Whew! At least the 
JVM stopped it.
arrays vs. 
ArrayLists


collections 
with 
generics
you are here
4
573
Wouldn’t it be dreamy if there were 
a way to still use polymorphic collection 
types as method arguments, so that my 
veterinary program could take Dog lists 
and Cat lists? That way I could loop through 
the lists and call their immunize() method, 
but it would still have to be safe so that you 
couldn’t add a Cat in to the Dog list. But I 
guess that’s just a fantasy...


574
 
chapter 16
Wildcards to the rescue
It looks unusual, but there is a way to create a method argument that 
can accept an ArrayList of any Animal subtype. The simplest way is to 
use a wildcard—added to the Java language explicitly for this reason.
public void takeAnimals(
ArrayList<
?
 extends Animal>
animals) {
for(Animal a: animals) {
a.eat();
}
}
So now you’re wondering, “What’s the difference? Don’t you have 
the same problem as before? The method above isn’t doing 
anything dangerous—calling a method any Animal subtype is 
guaranteed to have—but can’t someone still change this to add a 
Cat to the animals list, even though it’s really an ArrayList
And since it’s not checked again at runtime, how is this any 
different from declaring it without the wildcard?”
And you’d be right for wondering. The answer is NO. When you 
use the wildcard in your declaration, the compiler won’t let 
you do anything that adds to the list!
When you use a wildcard in your method 
argument, the compiler will STOP you from doing 
anything that could hurt the list referenced by the 
method parameter.
You can still invoke methods on the elements in 
the list, but you cannot add elements to the list.
In other words, you can do things 
with the list 
elements, but you can’t put 
new things in the list. 
So you’re safe at runtime, because the compiler 
won’t let you do anything that might be horrible at 
runtime.
So, this is OK inside takeAnimals():
for(Animal a: animals) {
a.eat();
}
But THIS would not compile:
animals.add(new Cat());
Remember, the keyword “extends” 
here means either extends OR 
implements depending on the 
type. So if you want to take 
an ArrayList of types that 
implement the Pet interface, 
you’d declare it as:
ArrayList

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   527   528   529   530   531   532   533   534   ...   555




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