— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

ArrayList
animals) {
animals.add(new Cat());
}
Yikes!! We just stuck a Cat in what 
might be a Dogs-only ArrayList.
So that’s the problem. There’s certainly nothing wrong with adding a
Cat to an ArrayList, and that’s the whole point of having an 
ArrayList of a supertype like Animal—so that you can put all types of 
animals in a single Animal ArrayList. 
But if you passed a Dog ArrayList—one meant to hold ONLY Dogs—
to this method that takes an Animal ArrayList, then suddenly you’d 
end up with a Cat in the Dog list. The compiler knows that if it lets 
you pass a Dog ArrayList into the method like that, someone could, 
at runtime, add a Cat to your Dog list. So instead, the compiler just 
won’t let you take the risk. 
If you declare a method to take ArrayList it can take ONLY an 
ArrayList, not ArrayList or ArrayList.


572
 
chapter 16
Wait a minute... if this is why they won’t let 
you pass a Dog ArrayList into a method that 
takes an Animal ArrayList—to stop you from 
possibly putting a Cat in what was actually a Dog list, 
then why does it work with 
arrays? Don’t you have 
the same problem with arrays? Can’t you still add 
a Cat object to a Dog[] ?
Array types are checked again at 
runtime, but collection type checks 
happen only when you compile
Let’s say you do add a Cat to an array declared as Dog[] (an array that 
was passed into a method argument declared as Animal[], which is a 
perfectly legal assignment for arrays).
public void go() {
Dog[] dogs = {new Dog(), new Dog(), new Dog()};
takeAnimals(dogs);
}
public void takeAnimals(Animal[] animals) {

Download 40,57 Mb.

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