Java for Fucking Idiots: Learn the Basics of Java Programming Without any experience!



Download 3,91 Mb.
Pdf ko'rish
bet67/70
Sana02.04.2022
Hajmi3,91 Mb.
#524999
1   ...   62   63   64   65   66   67   68   69   70
Bog'liq
Java for Fucking Idiots

class
Golem
extends
Monster {
Golem() {
health
=
100
;
damage
=
9
;
}
}
Just like before, with the Goblin, we don’t need to define those 
health
and
damage
properties again; the parent already did. We’re just assigning them
values of our own.
OK, one more. This one will do more than just have special health and
damage; it will do something different when it attacks!
class
Zombie
extends
Monster {
Zombie() {
damage
=
3
;
}
void
attack(Monster monsterToAttack) {
super
.attack(monsterToAttack);
health
+=
damage
;
}
}
Look at its attack method! Not only does it attack another monster, but it
heals itself when it does damage!! It’s a zombie, get it? It’s consuming
brains or something? Hah!
Super…
You probably noticed something a bit strange in the Zombie’s attack
method. Instead of just healing itself, as zombies might do, it says
super.attack()
first. What’s that business?
Child classes that want to specialize how they behave (their methods) may
want to do some stuff in addition to what their parent does. Those child


classes must tell the parent to do its thing first.
When a method first calls a super method, that is the way of saying “I’m
first going to delegate to my parent here, they know what to do.” You know,
that thing you never do in real life.
On the other hand, child classes that want to completely replace the parent
functionality can simply never call their parents using 
super
. If we did that
in the Zombie scenario, the zombie would heal itself but deal no damage
(because the parent Monster method’s code was never ran).
Fight to the death

Download 3,91 Mb.

Do'stlaringiz bilan baham:
1   ...   62   63   64   65   66   67   68   69   70




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