— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet210/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   206   207   208   209   210   211   212   213   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

216
 
chapter 8
Wait a minute... what good 
is a Dog if it comes out of an 
ArrayList and it can’t do 
any Dog things? There’s gotta be a 
way to get the Dog back to a state 
of Dogness...
I hope it doesn’t hurt. 
And what’s so wrong with 
staying an Object? OK, I can’t 
fetch, sure, but I can give you 
a real nice hashcode.
casting 
objects
Casting an object reference 
back to its 
real type.
Object
o
It’s really still a Dog object, but if you want to call 
Dog-specific methods, you need a reference declared 
as type Dog. If you’re sure* the object is really a 
Dog, you can make a new Dog reference to it by 
copying the Object reference, and forcing that 
copy to go into a Dog reference variable, using a 
cast (Dog). You can use the new Dog reference to 
call 
Dog methods.
Object o = al.get(index);
Dog d = (Dog) o;
d.roam();
Object
o
Dog object
Dog
d
*If you’re not sure it’s a Dog, you can use the 
instanceof
operator to check. Because if 
you’re wrong when you do the cast, you’ll get a 
ClassCastException at runtime and come to a 
grinding halt.
if (o instanceof Dog) {
Dog d = (Dog) o;
}
Dog object
cast the Object back to 
a Dog we know is there.
Cast the so-called ‘Object’ (but 
we know he’s actually a Dog) to 
type Dog, so that you can treat 
him like the Dog he really is.


interfaces and 
polymorphism
you are here
4
217
When you write a class, you almost always expose some 
of the methods to code outside the class. To expose 
method means you make a method accessible, usually by 
marking it public. 
Imagine this scenario: you’re writing code for a small 
business accounting program. A custom application 
for “Simon’s Surf Shop”. The good re-
user that you are, you found an Account 
class that appears to meet your needs 
perfectly, according to its documentation
anyway. Each account instance represents 
an individual customer’s account with the 
store. So there you are minding your own 
business invoking the credit() and debit() 
methods on an account object when you realize you 
need to get a balance on an account. No problem—
there’s a getBalance() method that should do nicely.
Except... when you invoke the getBalance() method, 
the whole thing blows up at runtime. Forget the 
documentation, the class does not have that method. 
Yikes!
But that won’t happen to you, because everytime you 
use the dot operator on a reference (a.doStuff()), the 
compiler looks at the reference type (the type ‘a’ was 
declared to be) and checks that class to guarantee the 
class has the method, and that the method does indeed 
take the argument you’re passing and return the kind of 
value you’re expecting to get back. 

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   206   207   208   209   210   211   212   213   ...   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