— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet96/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   92   93   94   95   96   97   98   99   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

theCat.height = 0; 
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.
yikes! We can’t 
let this happen!
real developers 
encapsulate
public void setHeight(int ht) {
if (ht > 9) {
height = ht;
}
}
By forcing everybody to call a setter 
method, we can protect the cat from 
unacceptable size changes.
We put in checks 
to guarantee a 
minimum cat height.


you are here
4
methods 
use 
instance variables
81
“Sadly, Bill forgot to 
encapsulate his Cat class and 
ended up with a flat cat.”
(overheard at the water cooler).
Hide the data
Yes it is that simple to go from 
an implementation that’s just 
begging for bad data to one 
that protects your data and 
protects your right to modify 
your implementation later.
OK, so how exactly do you 
hide the data? With the 
public 
and 
private 
access modifiers. You’re 
familiar with 
public
–we use 
it with every main method.
Here’s an encapsulation 
starter rule of thumb (all stan-
dard disclaimers about rules 
of thumb are in effect): mark 
your instance variables private 
and provide public getters 
and setters for access control. 
When you have more design 
and coding savvy in Java, you 
will probably do things a little 
differently, but for now, this 
approach will keep you safe.

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   92   93   94   95   96   97   98   99   ...   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