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


■ 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 214



Download 21,46 Mb.
Pdf ko'rish
bet438/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   434   435   436   437   438   439   440   441   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

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



214

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




// Remember! Person extends Object.

class Person

{

// Public only for simplicity. Properties and private data

// would obviously be preferred.

public string fName;

public string lName;

public byte personAge;

public Person(string firstName, string lastName, byte age)

{

fName = firstName;



lName = lastName;

personAge = age;

}

public Person(){}



}

Overriding System.Object.ToString()

Many classes (and structures) that you create can benefit from overriding ToString() in order to

return a string textual representation of the type’s current state. This can be quite helpful for pur-

poses of debugging (among other reasons). How you choose to construct this string is a matter of

personal choice; however, a recommended approach is to separate each name/value pair with

semicolons and wrap the entire string within square brackets (many types in the .NET base class

libraries follow this approach). Consider the following overridden ToString() for our Person class:

public override string ToString()

{

string myState;



myState = string.Format("[First Name: {0}; Last Name: {1}; Age: {2}]",

fName, lName, personAge);

return myState;

}

This implementation of ToString() is quite straightforward, given that the Person class only



has three pieces of state data. However, always remember that a proper ToString() override should

also account for any data defined up the chain of inheritance. When you override ToString() for a

class extending a custom base class, the first order of business is to obtain the ToString() value

from your parent using base. Once you have obtained your parent’s string data, you can append the

derived class’s custom information.


Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   434   435   436   437   438   439   440   441   ...   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