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



Download 21,46 Mb.
Pdf ko'rish
bet410/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   406   407   408   409   410   411   412   413   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition



Note

Methods that have been marked with the 

virtual

keyword are (not surprisingly) termed virtual methods.

When a subclass wishes to change the implementation details of a virtual method, it does so

using the override keyword. For example, the SalesPerson and Manager could override GiveBonus()

as follows (assume that PTSalesPerson will not override GiveBonus() and therefore simply inherit

the version defined by SalesPerson):

class SalesPerson : Employee

{

...



// A salesperson's bonus is influenced by the number of sales.

public override void GiveBonus(float amount)

{

int salesBonus = 0;



if (numberOfSales >= 0 && numberOfSales <= 100)

{ salesBonus = 10; }

else

{

if (numberOfSales >= 101 && numberOfSales <= 200)



{

salesBonus = 15;

}

else


{ salesBonus = 20; }

}

base.GiveBonus(amount * salesBonus);



}

}

class Manager : Employee



{

...


public override void GiveBonus(float amount)

{

base.GiveBonus(amount);



Random r = new Random();

numberOfOptions += r.Next(500);

}

}

Notice how each overridden method is free to leverage the default behavior using the base key-



word. In this way, you have no need to completely reimplement the logic behind GiveBonus(), but

can reuse (and possibly extend) the default behavior of the parent class.

Also assume that the current DisplayStats() method of the Employee class has been declared

virtually. By doing so, each subclass can override this method to account for displaying the number

of sales (for salespeople) and current stock options (for managers). For example, consider the

Manager’s version of the DisplayStats() method (the SalesPerson class would implement

DisplayStats() in a similar manner):

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



200

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




public override void DisplayStats()

{

base.DisplayStats();



Console.WriteLine("Number of Stock Options: {0}", numberOfOptions);

}

Now that each subclass can interpret what these virtual methods means to itself, each object



instance behaves as a more independent entity:

static void Main(string[] args)

{

Console.WriteLine("***** The Employee Class Hierarchy *****\n");




Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   406   407   408   409   410   411   412   413   ...   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