— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet88/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   84   85   86   87   88   89   90   91   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

72
 
chapter 4
objects have 
state 
and
 behavior
A class is the blueprint for an object. When you 
write a class, you’re describing how the JVM 
should make an object of that type. You already 
know that every object of that type can have 
different instance variable values. But what about 
the methods? 
Can every object of that type have different 
method behavior? 
Well... sort of.*
Every instance of a particular class has the same 
methods, but the methods can behave differently 
based on the value of the instance variables. 
The Song class has two instance variables, title 
and artist. The play() method plays a song, but 
the instance you call play() on will play the song
represented by the value of the title instance 
variable for that instance. So, if you call the play() 
method on one instance you’ll hear the song 
Politik”, while another instance plays “Darkstar”. 
The method code, however, is the same.
void play() {
soundPlayer.playSound(title);
}
Song
title
artist
setTitle()
setArtist()
play()
instance
variables
(state)
methods
(behavior)
knows
does
*Yes, another stunningly clear answer!
Remember: a class describes what an 
object knows and what an object does
Politik
Coldplay
Sing
Travis
Darkstar
Grateful
Dead
My Way
Sinatra
My Way
Sex Pistols
Song
t2
t2.play();
Song
s3
s3.play();
Calling play() on this instance 
will cause “Sing” to play. 
Calling play() on this instance 
will cause “My Way” to play. 
(but not the Sinatra one)

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   84   85   86   87   88   89   90   91   ...   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