— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet505/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   501   502   503   504   505   506   507   508   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

 


void sort
(List 
list
)
The sort() method can take only lists 
of
Comparable
objects.
Song is NOT a subtype of 
Comparable, so you cannot sort() 
the list of Songs.
At least not yet...
This says “Whatever ‘T’ is must 
be of type Comparable.”
(Ignore this part for now. But 
if you can’t, it just means 
that the type parameter for 
Comparable must be of type T 
or one of T’s supertypes.)
You can pass in only a List (or 
subtype of list, like ArrayList) 
that uses a parameterized type 
that “extends Comparable”.
So here we are, trying to read the sort() method docs to fi nd 
out why it was OK to sort a list of Strings, but not a 
list of Song objects. And it looks like the answer is...
Revisiting the sort( ) method
Um... I just checked the docs for 
String, and String doesn’t EXTEND 
Comparable--it IMPLEMENTS it. 
Comparable is an interface. So it’s nonsense 
to say extends Comparable>. 
public fi nal class 
String
 extends Object 
implements
 Serializable,

Comparable
, CharSequence


548
 
chapter 16
The Java engineers had to give you a way to put a constraint 
on a parameterized type, so that you can restrict it to, say, only 
subclasses of Animal. But you also need to constrain a type to 
allow only classes that implement a particular interface. So 
here’s a situation where we need one kind of syntax to work 
for both situations—inheritance and implementation. In other 
words, that works for both extends and implements.
And the winning word was... extends. But it really means “is-a”, 
and works regardless of whether the type on the right is an 
interface or a class.
In generics, “extends” means 
“extends or implements”

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   501   502   503   504   505   506   507   508   ...   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