— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

550
 
chapter 16
The new, improved, comparable Song class
We decided we want to sort by title, so we implement the compareTo() 
method to compare the title of the Song passed to the method against 
the title of the song on which the compareTo() method was invoked. 
In other words, the song running the method has to decide how its 
title compares to the title of the method parameter.
Hmmm... we know that the String class must know about alphabetical 
order, because the sort() method worked on a list of Strings. We know 
String has a compareTo() method, so why not just call it? That way, we 
can simply let one title String compare itself to another, and we don’t 
have to write the comparing/alphabetizing algorithm!
class 
Song
 
implements Comparable 
{
String title;
String artist;
String rating;
String bpm;


 
public int compareTo(Song s) {
return title.compareTo(s.getTitle());
}
Song(String t, String a, String r, String b) {
title = t;
artist = a;
rating = r;
bpm = b;
}
public String getTitle() {
return title;
}
public String getArtist() { 
return artist;
}
public String getRating() {
return rating;
}
public String getBpm() {
return bpm;
}

public String toString() {
return title; 

}
Usually these match...we’re specifying the type that 
the implementing class can be compared against. 
This means that Song objects can be compared to 
other Song objects, for the purpose of sorting.
Simple! We just pass the work 
on to the title String objects, 
since we know Strings have a 
compareTo() method.
The sort() method sends a Song to compareTo() 
to see how that Song compares to the Song on 
which the method was invoked.
File Edit Window Help Ambient

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   504   505   506   507   508   509   510   511   ...   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