— Scott McNealy, Sun Microsystems Chairman, President and ceo


instantiating the class decide the type of things that class will use?



Download 40,57 Mb.
Pdf ko'rish
bet500/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   496   497   498   499   500   501   502   503   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

instantiating the class decide the type of things that class will use?
A: 
You probably won’t do much of that. Think about it—the API 
designers made an entire library of collections classes covering most of 
the data structures you’d need, and virtually the only type of classes that 
really need to be generic are collection classes. In other words, classes 
designed to hold other elements, and you want programmers using it to 
specify what type those elements are when they declare and instantiate 
the collection class. 
Yes, it is possible that you might want to create generic classes, but that’s 
the exception, so we won’t cover it here. (But you’ll figure it out from the 
things we do cover, anyway.)


542
 
chapter 16
Using generic CLASSES
Since ArrayList is our most-used generified type, we’ll 
start by looking at its documentation. They two key areas 
to look at in a generified class are:
1) The class declaration
3) The method declarations that let you add elements
public boolean add(
E
 o)
// more code
public class ArrayList

 extends AbstractList

 implements List

... {


}

Understanding ArrayList documentation


(Or, what’s the true meaning of “E”?)
Think of “E” as a stand-in for 
“the type of element you want 
this collection to hold and 
return.” (E is for Element.)
The “E” is a placeholder for the 
REAL type you use when you 
declare and create an ArrayList
ArrayList is a subclass of AbstractList, 
so whatever type you specify for the 
ArrayList is automatically used for the 
type of the AbstractList.
The type (the value of
becomes the type of the List 
interface as well.
Here’s the important part! Whatever “E” is 
determines what kind of things you’re allowed 
to add to the ArrayList.
The “E” represents the type used to create an instance 
of ArrayList. When you see an “E” in the ArrayList 
documentation, you can do a mental find/replace to 
exchange it for whatever you use to instantiate 
ArrayList. 
So, new ArrayList means that “E” becomes “Song”, 
in any method or variable declaration that uses “E”.

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   496   497   498   499   500   501   502   503   ...   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