— Scott McNealy, Sun Microsystems Chairman, President and ceo


public class AnimalTestDrive{



Download 40,57 Mb.
Pdf ko'rish
bet198/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   194   195   196   197   198   199   200   201   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

public class AnimalTestDrive{
public static void main (String[] args) {
MyAnimalList list = new MyAnimalList();
Dog a = new Dog();
Cat c = new Cat();
list.add(a);
list.add(c);
}
}

File Edit Window Help Harm


% java AnimalTestDrive
Animal added at 0
Animal added at 1
Don’t panic. We’re not making a 
new Animal object; we’re making a 
new array object, of type Animal. 
(Remember, you cannot make a new 
instance of an abstract type, but 
you CAN make an array object 
declared to HOLD that type.)


208
 
chapter 8
You know where this is heading. We want to change the 
type of the array, along with the add() method argument, to 
something above Animal. Something even more generic, more 
abstract than Animal. But how can we do it? We don’t have 
superclass for Animal.
Then again, maybe we do...
Remember those methods of ArrayList? 
Look how the remove, contains, and 
indexOf method all use an object of type... 
Object!
Every class in Java extends 
class Object. 
Class Object is the mother of all classes; it’s 
the superclass of everything.
ve
rsion
3
What about non-Animals? Why not make 
a class generic enough to take anything?
Many of the ArrayList methods use the 
ultimate polymorphic type, Object. Since 
every class in Java is a subclass of Object, 
these ArrayList methods can take anything
!
(Note: as of Java 5.0, the get() and add() 
methods actually look a little different 
than the ones shown here, but for now this 
is the way to think about it. We’ll get into 
the full story a little later.)

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   194   195   196   197   198   199   200   201   ...   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