This print for content only-size & color not accurate 7" X 9-1/4" / casebound / malloy


// Make an array of Shape-compatible objects



Download 21,46 Mb.
Pdf ko'rish
bet424/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   420   421   422   423   424   425   426   427   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Make an array of Shape-compatible objects.

Shape[] myShapes = {new Hexagon(), new Circle(), new Hexagon("Mick"),

new Circle("Beth"), new Hexagon("Linda")};

// Loop over each item and interact with the

// polymorphic interface.

foreach (Shape s in myShapes)

{

s.Draw();



}

Console.ReadLine();

}

Figure 6-11 shows the output.



C H A P T E R   6  

U N D E R S TA N D I N G   I N H E R I TA N C E  A N D   P O LY M O R P H I S M



207

8849CH06.qxd  10/1/07  10:35 AM  Page 207




Figure 6-11.

Polymorphism in action

This Main() method illustrates polymorphism at its finest. Although it is not possible to directly

create an abstract base class (the Shape), you are able to freely store references to any subclass with

an abstract base variable. Therefore, when you are creating an array of Shapes, the array can hold

any object deriving from the Shape base class (if you attempt to place Shape-incompatible objects

into the array, you receive a compiler error).

Given that all items in the myShapes array do indeed derive from Shape, we know they all sup-

port the same polymorphic interface (or said more plainly, they all have a Draw() method). As you

iterate over the array of Shape references, it is at runtime that the underlying type is determined. At

this point, the correct version of the Draw() method is invoked.

This technique also makes it very simple to safely extend the current hierarchy. For example,

assume we derived five more classes from the abstract Shape base class (Triangle, Square, etc.). Due

to the polymorphic interface, the code within our for loop would not have to change in the slightest

as the compiler enforces that only Shape-compatible types are placed within the myShapes array.




Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   420   421   422   423   424   425   426   427   ...   1830




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