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



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

new
House();
System.
out
.println(coloredHouse.
color
);


The answer? It’s…. the … color…. White! Yep, the constructor’s code runs
after the properties’ values are assigned.
You can verify this by adding some Sysouts.
class
House {
String
color
=
"Gray"
;
House() {
System.
out
.println(
color
);
color
=
"White"
;
System.
out
.println(
color
);
}
}
The result?
>
Gray
> White
Bob had Gray on his blueprint, but when he went to start building, he
decided on White instead.
Brain teaser over. Your brain is no longer being teased.
Brain teaser #2!
Haha, gotcha! We’re back with another one.
What if we create a constructor that looks like this?
class
House {
String
color
;
House(String color) {
color = color;
}
}
What the hell even happens on a line of code where we say that “color”
should be assigned the value of “color”??? color = color???


The answer is nothing happens. The house’s color property is left alone.
The custom color value that we’re providing Bob is re-assigned the same
value. That’s fucking stupid.
We can fix this by specifying that we want to assign the custom 
color
to our
property color .
class
House {
String
color
;
House(String color) {
this
.
color
= color;
}
}
Just like you can access an object’s properties and methods with “dot
notation”, a.k.a. a period, an object can refer to its own properties and
methods using this followed by the dot.
Which is great in this case where we need to differentiate between our
property and the parameter, both with the same name.
¿Porque no los dos?
It’s time to introduce you to a concept called engineer overkill.
Wait, that’s not it….
Constructor strain?
No, no…. constructor…overload? Yep, that’s it! Constructor overloading !
Fortunately for me, the Java developer, I don’t have to have just one
constructor. I can have as many, or as few, as I want!
Unfortunately for you, it’s another thing you should learn.
Treat your eyes to this: two constructors!!

Download 3,91 Mb.

Do'stlaringiz bilan baham:
1   ...   39   40   41   42   43   44   45   46   ...   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