— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet166/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   162   163   164   165   166   167   168   169   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

hunger 
– an int representing the hunger level of the animal. 
It changes depending on when (and how much) the 
animal eats.
boundaries 
– values representing the height and width of 
the ‘space’ (for example, 640 x 480) that the animals will 
roam around in.
location 
– the X and Y coordinates for where the animal is 
in the space.
We have four 
methods
:
makeNoise
() – behavior for when the animal is supposed to 
make noise.
eat() 
– behavior for when the animal encounters its 
preferred food source, meat or grass.
sleep() 
– behavior for when the animal is considered asleep. 
roam() 
– behavior for when the animal is not eating or 
sleeping (probably just wandering around waiting to bump 
into a food source or a boundary).
Using inheritance to avoid 
duplicating code in subclasses
size
picture
food
prey
Dog
size
picture
food
prey
Wolf
size
picture
food
prey
Cat
size
picture
food
prey
Tiger
size
picture
food
prey
Hippo
size
picture
food
prey
Lion


172
 
chapter 7
Assume that we all agree on one thing: the instance 
variables will work for all Animal types. A lion will 
have his own value for picture, food (we’re thinking 
meat), hunger, boundaries, and location. A hippo 
will have different values for his instance variables, 
but he’ll still have the same variables that the other 
Animal types have. Same with dog, tiger, and so on. 
But what about behavior?
Which methods should we override?
Does a lion make the same noise as a dog? Does 
a cat eat like a hippo? Maybe in your version, but 
in ours, eating and making noise are Animal-type-
specifi c. We can’t fi gure out how to code those 
methods in such a way that they’d work for any 
animal. OK, that’s not true. We could write the 
makeNoise() method, for example, so that all it does 
is play a sound fi le defi ned in an instance variable 
for that type, but that’s not very specialized. Some 
animals might make different noises 
for different situations (like one 
for eating, and another when 
bumping into an enemy, etc.)
So just as with the Amoeba 
overriding the Shape class rotate() 
method, to get more amoeba-specifi c (in 
other words, unique) behavior, we’ll have 
to do the same for our Animal subclasses.
Do all animals eat the same way?
Animal
I’m one bad*ss
plant-eater.
In the dog 
community, barking is an 
important part of our cultural 
identity. We have a unique sound, 
and we want that diversity to 
be recognized and respected.
picture
food
hunger
boundaries
location
makeNoise()
eat()
sleep()
roam()
We better override these two methods, eat() 
and makeNoise(), so that each animal type can 
define its own specific behavior for eating and 
making noise. For now, it looks like sleep() and 
roam() can stay generic.

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   162   163   164   165   166   167   168   169   ...   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