— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

public Duck() {
// constructor code goes here
}
Its name is the same as the 
class name. That’s mandatory.

constructor
 has the 
code that runs when you 
instantiate an object. In 
other words, the code that 
runs when you say 
new
 on 
a class type. 
Every class you create has 
a constructor, even if you 
don’t write it yourself.


242
 
chapter 9
constructing a new 
Duck
Construct a Duck
The key feature of a constructor is that it runs 
before the object can be assigned to a reference. 
That means you get a chance to step in and 
do things to get the object ready for use. In 
other words, before anyone can use the remote 
control for an object, the object has a chance to 
help construct itself. In our Duck constructor, 
we’re not doing anything useful, but it still 
demonstrates the sequence of events.
If it Quacks like a 
constructor...
public class UseADuck {
public static void main (String[] args) {
Duck d = new Duck();
}
}
public class Duck {
public Duck() {
System.out.println(“Quack”);
}
}
File Edit Window Help Quack
% java UseADuck
Quack
Constructor code.
This calls the Duck 
constructor.
The constructor gives 
you a chance to step into 
the middle of
new
.
Sharpen your pencil
A constructor lets you jump into the middle 
of the object creation step—into the middle 
of 
new
. Can you imagine conditions where 
that would be useful? Which of these might 
be useful in a Car class constructor, if the Car 
is part of a Racing Game? Check off the ones 
that you came up with a scenario for.
®
Increment a counter to track how many objects of this class type 
have been made.
®
Assign runtime-specific state (data about what’s happening NOW).
®
Assign values to the object’s important instance variables.
®
Get and save a reference to the object that’s creating the new object.
®
Add the object to an ArrayList.
®
Create HAS-A objects.
®
________________________________________ (your idea here)



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   230   231   232   233   234   235   236   237   ...   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