C# 0 The Complete Reference


B  is inherited by  D



Download 4,07 Mb.
Pdf ko'rish
bet366/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   362   363   364   365   366   367   368   369   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

B

 is inherited by 



D

 and because 



i

 and 


j

 are declared as 



protected

 in 


B

, the 


Setk( )

 method can access them. If 



i

 and 


j

 had been declared as private by 



B

, then 


D

would not have access to them, and the program would not compile.

Like

public

 and 


private

,

protected

 status stays with a member no matter how many 

layers of inheritance are involved. Therefore, when a derived class is used as a base class for 

another derived class, any protected member of the initial base class that is inherited by the 

first derived class is also inherited as protected by a second derived class.

Although

protected

 access is quite useful, it doesn’t apply in all situations. For example, 

in the case of 

TwoDShape

 shown in the preceding section, we specifically want the 



Width

and


Height

 values to be publicly accessible. It’s just that we want to manage the values they 

are assigned. Therefore, declaring them 

protected

 is not an option. In this case, the use of 

properties supplies the proper solution by controlling, rather than preventing, access. 

Remember, use 



protected

 when you want to create a member that is accessible throughout a 

class hierarchy, but otherwise private. To manage access to a value, use a property.

Constructors and Inheritance

In a hierarchy, it is possible for both base classes and derived classes to have their own 

constructors. This raises an important question: What constructor is responsible for building 

an object of the derived class? The one in the base class, the one in the derived class, or both? 

Here is the answer: The constructor for the base class constructs the base class portion of the 

object, and the constructor for the derived class constructs the derived class part. This makes 

sense because the base class has no knowledge of or access to any element in a derived 

class. Thus, their construction must be separate. The preceding examples have relied upon 

the default constructors created automatically by C#, so this was not an issue. However, in 

practice, most classes will define constructors. Here you will see how to handle this situation.

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   362   363   364   365   366   367   368   369   ...   1096




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