— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet81/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   77   78   79   80   81   82   83   84   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

you are here
4
primitives 
and 
references
61
Dog
Dog
name
bark()
eat()
chaseCat()
Control your Dog
(with a reference variable)
Dog fi do = new Dog();
fi do.name = “Fido”;
Dog obje
ct
name
String
We created a Dog object and 
used the dot operator on the 
reference variable fi do to access 
the name variable.*
We can use the fi do reference 
to get the dog to bark() or 
eat() or chaseCat().
fi do.bark();
fi do.chaseCat();
fido
What happens if the Dog is in
a Dog array?
We know we can access the Dog’s 
instance variables and methods using 
the dot operator, but on what? 
When the Dog is in an array, we don’t 
have an actual variable name (like 
fi do
). Instead we use array notation and 
push the remote control button (dot 
operator) on an object at a particular 
index (position) in the array:
Dog[] myDogs = new Dog[3];
myDogs[0] = new Dog();
myDogs[0].name = “Fido”;
myDogs[0].bark();
*Yes we know we’re not demonstrating encapsulation here, but we’re 
trying to keep it simple. For now. We’ll do encapsulation in chapter 4.
Java cares about type.
Once you’ve dec
lared an array, y
ou 
can’t put anything in it e
xcept things 
that are of the dec
lared array type. 
For example, you can’t put a Cat into a Dog 
array (it would be pretty awful if someone 
thinks that only Dogs are in the array, so 
they ask each one to bark, and then to their 
horror discover there’s a cat lurking.) And 
you can’t stick a 
double 
into an 
int 
array 
(spillage, remember?). You can, however, 
put a 
byte 
into an 
int 
array, because a 
byte 
will always fi t into an 
int
-sized cup. 
This is known as an implicit widening. We’ll 

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   77   78   79   80   81   82   83   84   ...   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