— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet197/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   193   194   195   196   197   198   199   200   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

Concrete
 
 
 
Sample class
 
 
Abstract
golf course simulation 
Tree
tree nursery application
____________________
House
architect application
satellite photo application
Town
_____________________
____________________
Football Player
coaching application
____________________
Chair 
_____________________
____________________
Customer 
_____________________
____________________
Sales Order 
_____________________
____________________
Book 
_____________________
____________________
Store 
_____________________
____________________
Supplier
_____________________
____________________
Golf Club 
_____________________
____________________
Carburetor 
_____________________
____________________
Oven 
_____________________
Abstract vs. Concrete Classes


206
 
chapter 8
polymorphism 
examples
Polymorphism in action
Let’s say that we want to write our own kind of list class, one that will hold 
Dog objects, but pretend for a moment that we don’t know about the 
ArrayList class. For the fi rst pass, we’ll give it just an add() method. We’ll use 
a simple Dog array (Dog []) to keep the added Dog objects, and give it a 
length of 5. When we reach the limit of 5 Dog objects, you can still call the 
add() method but it won’t do anything. If we’re not at the limit, the add() 
method puts the Dog in the array at the next available index position, then 
increments that next available index (nextIndex).
public class 
MyDogList
 {
private Dog [] dogs = new Dog[5];
Animal_a)_{__if_(nextIndex__animals[nextIndex]_=_a;'> private int nextIndex = 0;

public void add(Dog d) {
if (nextIndex < dogs.length) {
dogs[nextIndex] = d;
System.out.println(“Dog added at “ + nextIndex);
nextIndex++;
}
}
}
MyDogList
Dog[] dogs
int nextIndex
add(Dog d)
Use a plain old Dog array 
behind the scenes.
We’ll increment this each 
time a new Dog is added.
If we’re not already at the limit 
of the dogs array, add the Dog 
and print a message.
increment, to give us the 
next index to use
Building our own Dog-specifi c list 
(Perhaps the world’s worst attempt at making our 
own ArrayList kind of class, from scratch.)
ve
rsion
1


interfaces and 
polymorphism
you are here
4
207
public class MyAnimalList {
private Animal[] animals = new Animal[5];
private int nextIndex = 0;

public void add(Animal a) {
if (nextIndex < animals.length) {
animals[nextIndex] = a;
System.out.println(“Animal added at “ + nextIndex);
nextIndex++;

}
}
}
MyAnimalList
Animal[] animals
int nextIndex
add(Animal a)
Building our own Animal-specifi c list 
ve
rsion
2
Uh-oh, now we need to keep Cats, too.
We have a few options here:
1) Make a separate class, MyCatList, to hold Cat objects. Pretty clunky.
2) Make a single class, DogAndCatList, that keeps two different arrays as instance 
variables and has two different add() methods: addCat(Cat c) and addDog(Dog 
d). Another clunky solution.
3) Make heterogeneous AnimalList class, that takes any kind of Animal subclass 
(since we know that if the spec changed to add Cats, sooner or later we’ll have 
some other kind of animal added as well). We like this option best, so let’s change 
our class to make it more generic, to take Animals instead of just Dogs. We’ve 
highlighted the key changes (the logic is the same, of course, but the type has 
changed from Dog to Animal everywhere in the code.

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   193   194   195   196   197   198   199   200   ...   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