— Scott McNealy, Sun Microsystems Chairman, President and ceo


for making a Duck—one where they supply the Duck



Download 40,57 Mb.
Pdf ko'rish
bet238/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   234   235   236   237   238   239   240   241   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

for making a Duck—one where they supply the Duck 
size (as the constructor argument) and one where they 
don’t specify a size and thus get your default Duck size.
You can’t do this cleanly with just a single constructor. 
Remember, if a method (or constructor—same rules) has 
a parameter, you must pass an appropriate argument when 
you invoke that method or constructor. You can’t just say, “If 
someone doesn’t pass anything to the constructor, then use 
the default size”, because they won’t even be able to compile 
without sending an int argument to the constructor call. You 
could do something clunkly like this:
But that means the programmer making a new Duck object has 
to know that passing a “0” is the protocol for getting the default 
Duck size. Pretty ugly. What if the other programmer doesn’t 
know that? Or what if he really does want a zero-size Duck? 
(Assuming a zero-sized Duck is allowed. If you don’t want 
zero-sized Duck objects, put validation code in the constructor 
to prevent it.) The point is, it might not always be possible 
to distinguish between a genuine “I want zero for the size” 
constructor argument and a “I’m sending zero so you’ll give 
me the default size, whatever that is” constructor argument.
public class Duck2 {
int size;
public Duck2() {
// supply default size
size = 27;
}
public Duck2(int duckSize) {
// use duckSize parameter
size = duckSize;
}
}
To make a Duck when you know the size:
Duck2 d = new Duck2(15);
To make a Duck when you do not know 
the size:
Duck2 d2 = new Duck2();
You really want TWO ways to 
make a new Duck:

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   234   235   236   237   238   239   240   241   ...   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