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



Download 17,18 Mb.
Pdf ko'rish
bet59/90
Sana22.06.2022
Hajmi17,18 Mb.
#693704
1   ...   55   56   57   58   59   60   61   62   ...   90
Bog'liq
(Head Up) Kathy Sierra Bert Bates - Head First Java, 3rd Edition Early Release-O\'Reilly Media, Inc. (2021)

class ElectricGuitar {
String brand;
int numOfPickups;
boolean rockStarUsesIt;
String getBrand() {


return brand;
}
void setBrand(String aBrand) {
brand = aBrand;
}
int getNumOfPickups() {
return numOfPickups;
}
void setNumOfPickups(int num) {
numOfPickups = num;
}
boolean getRockStarUsesIt() {
return rockStarUsesIt;
}
void setRockStarUsesIt(boolean yesOrNo) {
rockStarUsesIt = yesOrNo;

}
Encapsulation


Do it or risk humiliation and ridicule.
Until this most important moment, we’ve been committing one of the worst
OO faux pas (and we’re not talking minor violation like showing up
without the ‘B’ in BYOB). No, we’re talking Faux Pas with a capital ‘F’.
And ‘P’.
Our shameful transgression?
Exposing our data!
Here we are, just humming along without a care in the world leaving our
data out there for 
anyone
to see and even touch.
You may have already experienced that vaguely unsettling feeling that
comes with leaving your instance variables exposed.
Exposed means reachable with the dot operator, as in:
theCat.height = 27;


Think about this idea of using our remote control to make a direct change to
the Cat object’s size instance variable. In the hands of the wrong person, a
reference variable (remote control) is quite a dangerous weapon. Because
what’s to prevent:
This would be a Bad Thing. We need to build setter methods for all the
instance variables, and find a way to force other code to call the setters


rather than access the data directly.

Download 17,18 Mb.

Do'stlaringiz bilan baham:
1   ...   55   56   57   58   59   60   61   62   ...   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