— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

public class Duck {
int size;
public Duck(int duckSize) {
System.out.println(“Quack”);
size = duckSize;
System.out.println(“size is “ + size);
}
}
public class UseADuck {
public static void main (String[] args) {
Duck d = new Duck(42);
}
}
File Edit Window Help Honk
% java UseADuck
Quack
size is 42
Add an int parameter to the 
Duck constructor.
Use the argument value to set 
the size instance variable.
Pass a value to the 
constructor.
This time there’s only 
one statement. We make 
the new Duck and set 
its size 
in one statement.
Let the user make a new Duck 
and set the Duck’s size all in 
one call. The call to new.
The call to the Duck 
constructor.
*Not to imply that not all Duck state is not unimportant.
initializing object 
state


constructors and 
gc
you are here
4
245
Make it easy to make a Duck
Be sure you have a no-arg constructor
What happens if the Duck constructor takes an argument? 
Think about it. On the previous page, there’s only one Duck 
constructor—and it takes an int argument for the size of the 
Duck. That might not be a big problem, but it does make it 
harder for a programmer to create a new Duck object, especially 
if the programmer doesn’t know what the size of a Duck should 
be. Wouldn’t it be helpful to have a default size for a Duck, so 
that if the user doesn’t know an appropriate size, he can still 
make a Duck that works?
Imagine that you want Duck users to have TWO options 

Download 40,57 Mb.

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