Java for Fucking Idiots: Learn the Basics of Java Programming Without any experience!



Download 3,91 Mb.
Pdf ko'rish
bet41/70
Sana02.04.2022
Hajmi3,91 Mb.
#524999
1   ...   37   38   39   40   41   42   43   44   ...   70
Bog'liq
Java for Fucking Idiots

color
=
"Gray"
;
}
The blueprint has a default value here.
When the House is created , it will have that value. Yes, the line of code
that makes the color property “Gray” will not run until you create a House


object.
Speaking of which, I can ask Bob to actually build the House in a line of
code like this:
House stevesHouse =
new
House();
The right side is the important bit; it says make a new House!...and assign it
to the variable on the left.
See those two parentheses ()? With nothing inside of ‘em? That’s us telling
Bob to build a default, normal, vanilla, bland, unimportant house. The
house will be Gray.
System.
out
.println(stevesHouse.
color
);
Yep, that will say “Gray”! Also, don’t get me wrong; gray houses are totally
fine. Very adequate.
Default Constructors
Every class has a hidden Bob the Builder by default that looks like this:
class
House {
String
color
=
"Gray"
;
House() {}
}
A constructor is always the name of the class followed by parentheses .
Then some curly braces {}.
See the constructor in that example?
When a House object is created, the 
color
property is given the value
“Gray”. Then, the constructor gets to work, and …. well, this one doesn’t
do shit. No need to even write it out. But it’s always there!!
You might sometimes see people declare the House like this instead:
class
House {
String
color
;


House() {
color
=
"Gray"
;
}
}
Switch between this example and the previous one. Note the difference.
Well, there’s effectively no difference, but you can choose which way you
prefer. I like setting the property values inside the constructor (this one).
Getting it right from the start
Anyway, sure; Bob could build the house, paint it gray, and then you could
come and repaint the place yourself.
House stevesHouse =

Download 3,91 Mb.

Do'stlaringiz bilan baham:
1   ...   37   38   39   40   41   42   43   44   ...   70




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