— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

Object: 
The point to setters (and getters, too) is that 
you can change your mind later, 
without breaking anybody else’s code! 
Imagine if half the people in your com-
pany used your class with public instance variables, and one day you suddenly realized, 
“Oops– there’s something I didn’t plan for with that value, I’m going to have to switch to a 
setter method.” You break everyone’s code. The cool thing about encapsulation is that you 
get to change your mind. And nobody gets hurt. The performance gain from using variables 
directly is so miniscule and would rarely—if ever— be worth it.
This week’s interview:
An Object gets candid about encapsulation. 
Java Exposed


how 
objects 
behave
82
 
chapter 4
class GoodDog {
private int size;
public int getSize() {
return size;

public void setSize(int s) {
size = s;
}
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 GoodDogTestDrive {
public static void main (String[] args) {
GoodDog one = new GoodDog();
one.setSize(70); 
GoodDog two = new GoodDog();
two.setSize(8);
System.out.println(“Dog one: “ + one.getSize());
System.out.println(“Dog two: “ + two.getSize());
one.bark();
two.bark();
}
}
Encapsulating the 
GoodDog class
Make the instance 
variable private.
Make the getter and 
setter methods public.
Even though the methods don’t really 
add new functionality, the cool thing 
is that you can change your mind 
later. you can come back and make a 
method safer, faster, better.
GoodDog
size
getSize( )
setSize( )
bark( )
Any place where a 
Download 40,57 Mb.

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