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



Download 21,46 Mb.
Pdf ko'rish
bet348/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   344   345   346   347   348   349   350   351   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Car 'has-a' Radio

private Radio myRadio = new Radio();

public void TurnOnRadio(bool onOff)

{

// Delegate call to inner object.

myRadio.Power(onOff);

}

}



Notice that the object user has no clue that the Car class is making use of an inner Radio object.

static void Main(string[] args)

{

// Call is forwarded to Radio internally.

Car viper = new Car();

viper.TurnOnRadio(false);

}

C H A P T E R   5  



D E F I N I N G   E N C A P S U L AT E D   C L A S S  T Y P E S



161

8849CH05.qxd  10/22/07  1:34 PM  Page 161




The Role of Polymorphism

The final pillar of OOP is polymorphism. This trait captures a language’s ability to treat related

objects in a similar manner. Specifically, this tenant of an object-oriented language allows a base

class to define a set of members (formally termed the 



polymorphic interface) that are available to all

descendents. A class’s polymorphic interface is constructed using any number of 



virtual or abstract

members (see Chapter 6 for full details).

In a nutshell, a 

virtual member is a member in a base class that defines a default implementa-

tion that may be changed (or more formally speaking, 



overridden) by a derived class. In contrast, an

abstract method is a member in a base class that does not provide a default implementation, but

does provide a signature. When a class derives from a base class defining an abstract method, it



must be overridden by a derived type. In either case, when derived types override the members

defined by a base class, they are essentially redefining how they respond to the same request.

To preview polymorphism, let’s provide some details behind the shapes hierarchy shown in

Figure 5-8. Assume that the Shape class has defined a virtual method named Draw() that takes no

parameters. Given the fact that every shape needs to render itself in a unique manner, subclasses

(such as Hexagon and Circle) are free to override this method to their own liking (see Figure 5-9).



Figure 5-9.

Classical polymorphism

Once a polymorphic interface has been designed, you can begin to make various assumptions

in your code. For example, given that Hexagon and Circle derive from a common parent (Shape), an

array of Shape types could contain anything deriving from this base class. Furthermore, given that

Shape defines a polymorphic interface to all derived types (the Draw() method in this example), we

can assume each member in the array has this functionality.

Consider the following Main() method, which instructs an array of Shape-derived types to ren-

der themselves using the Draw() method:

class Program

{

static void Main(string[] args)



{

Shape[] myShapes = new Shape[3];

myShapes[0] = new Hexagon();

myShapes[1] = new Circle();

myShapes[2] = new Hexagon();

foreach (Shape s in myShapes)

{

s.Draw();



}

C H A P T E R   5  




Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   344   345   346   347   348   349   350   351   ...   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