Head First Java a brain-Friendly Guide Kathy Sierra and Bert Bates


methods use instance variable values



Download 17,18 Mb.
Pdf ko'rish
bet54/90
Sana22.06.2022
Hajmi17,18 Mb.
#693704
1   ...   50   51   52   53   54   55   56   57   ...   90
Bog'liq
(Head Up) Kathy Sierra Bert Bates - Head First Java, 3rd Edition Early Release-O\'Reilly Media, Inc. (2021)

methods use instance variable values
. Like, “if
dog is less than 14 pounds, make yippy sound, else...” or “increase weight
by 5”. 
Let’s go change some state.
Remember: a class describes what an object
knows and what an object does
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 “Havana”,
while another instance plays “Blinding Lights”. The method code, however,
is the same.
void play() { 
soundPlayer.playSound(title); 



Song t2 = new Song(); 
t2.setArtist(“Travis”); 
t2.setTitle(“Sing”); 
Song s3 = new Song(); 
s3.setArtist(“Sex Pistols”); 
s3.setTitle(“My Way”);


*Yes, another stunningly clear answer!
The size affects the bark


A small Dog’s bark is different from a big Dog’s bark.
The Dog class has an instance variable 
size
, that the 
bark()
method uses to
decide what kind of bark sound to make.
class Dog { 
int size; 
String name; 
void bark() { 
if (size > 60) { 
System.out.println(“Wooof! Wooof!”); 
} else if (size > 14) { 
System.out.println(“Ruff! Ruff!”); 
} else { 
System.out.println(“Yip! Yip!”); 


}
_____________________________________________________
class DogTestDrive { 
public static void main (String[] args) { 
Dog one = new Dog(); 
one.size = 70; 
Dog two = new Dog(); 
two.size = 8; 


Dog three = new Dog(); 
three.size = 35; 
one.bark(); 
two.bark(); 
three.bark(); 

}



Download 17,18 Mb.

Do'stlaringiz bilan baham:
1   ...   50   51   52   53   54   55   56   57   ...   90




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