— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet243/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   239   240   241   242   243   244   245   246   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

 
 
 
d[2] = new Duck(name, feathers); 
 
d[3] = new Duck(canFly);
d[4] = new Duck(3.3F, airspeed);
 
 
 
 
 
d[5] = new Duck(false);
d[6] = new Duck(airspeed, density);
}
}
 
 
 
 
 

 
 
 
 
class Duck {
int pounds = 6;
float floatability = 2.1F;
String name = “Generic”;
long[] feathers = {1,2,3,4,5,6,7}; 
 
boolean canFly = true;
int maxSpeed = 25;
 
public Duck() {
System.out.println(“type 1 duck”);
}
public Duck(boolean fly) {
canFly = fly;
System.out.println(“type 2 duck”);
}
public Duck(String n, long[] f) {
name = n;
feathers = f;
System.out.println(“type 3 duck”);
}
public Duck(int w, float f) {
pounds = w;
floatability = f;
System.out.println(“type 4 duck”);
}
public Duck(float density, int max) {
floatability = density;
maxSpeed = max;
System.out.println(“type 5 duck”);
}

 
Sharpen your pencil
Match the 
new Duck()
call with the constructor 
that runs when that Duck is instantiated. We did 
the easy one to get you started.
overloaded 
constructors


constructors and 
gc
you are here
4
249
What about superclasses? 
When you make a Dog, 
should the Canine 
constructor run too? 
If the superclass is abstract, 
should it even 
have a 
constructor?
We’ll look at this on the next 
few pages, so stop now and 
think about the implications of 
constructors and superclasses.
Nanoreview: four things to 
remember about constructors
1
A constructor is the code that runs when 
somebody says 
new
on a class type
2
A constructor must have the same name 
as the class, and 
no return type
3
If you don’t put a constructor in your class, 
the compiler puts in a default constructor. 
The default constructor is always a no-arg 
constructor.
4
You can have more than one constructor in your class, 
as long as the argument lists are different. Having 
more than one constructor in a class means you have 
overloaded constructors.
Duck d = new Duck();

public Duck(int size) { }
public Duck() { }
public Duck() { }
public Duck(int size) { }
public Duck(String name) { }
public Duck(String name, int size) { }
Doing all the Brain Barbells has been shown to produce a 42% increase in 
neuron size. And you know what they say, “Big neurons...”
there are no
Dumb Questions
Q:
Do constructors have to be 
public
?
A: 
No. Constructors can be 
public

private
, or default (which means no access 
modifier at all). We’ll look more at default  
access in chapter 16 and appendix B.
Q:
How could a private constructor 

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   239   240   241   242   243   244   245   246   ...   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