— Scott McNealy, Sun Microsystems Chairman, President and ceo


ArrayList dogs = new ArrayList()



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

ArrayList dogs = new ArrayList();
dogs.add(new Dog());
dogs.add(new Dog());
 takeAnimals(dogs); 
}
public void 
takeAnimals(ArrayList animals)
{
for(Animal a: animals) {
a.eat();
}
}
We know this line worked fine.
Make a Dog ArrayList and put a couple dogs in.
Passing in just ArrayList
%java TestGenerics3
TestGenerics3.java:21: takeAnimals(java.util.
ArrayList) in TestGenerics3 cannot be applied to 
(java.util.ArrayList)
takeAnimals(dogs);
^
1 error
File Edit Window Help CatsAreSmarter
When we compile it:
It looked so right, 
but went so wrong...
Will this work now that we changed 
from an array to an ArrayList?
polymorphism and 
generics


collections 
with 
generics
you are here
4
571
And I’m supposed to be OK with this? That 
totally screws my animal simulation where the 
veterinary program takes a list of any type of 
animal, so that a dog kennel can send a list of dogs, 
and a cat kennel can send a list of cats... now 
you’re saying I can’t do that if I use collections 
instead of arrays?
What could happen if it 
were allowed...
Imagine the compiler let you get away with that. It let you pass an 
ArrayList to a method declared as:
public void takeAnimals(
ArrayList
animals) {
for(Animal a: animals) {
a.eat();
}
}
There’s nothing in that method that looks harmful, right? After all, 
the whole point of polymorphism is that anything an Animal can 
do (in this case, the eat() method), a Dog can do as well. So what’s 
the problem with having the method call eat() on each of the Dog 
references? 
Nothing. Nothing at all.
There’s nothing wrong with that code. But imagine this code instead:
public void takeAnimals(

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   525   526   527   528   529   530   531   532   ...   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