C# 0 The Complete Reference


static . Controlling Access to Class Members



Download 4,07 Mb.
Pdf ko'rish
bet232/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   228   229   230   231   232   233   234   235   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

static

.

Controlling Access to Class Members

In its support for encapsulation, the class provides two major benefits. First, it links data 

with code. You have been taking advantage of this aspect of the class since Chapter 6. 

Second, it provides the means by which access to members can be controlled. It is this 

second feature that is examined here.

Although C#’s approach is a bit more sophisticated, in essence, there are two basic 

types of class members: public and private. A public member can be freely accessed by code 

defined outside of its class. This is the type of class member that we have been using up to 

this point. A private member can be accessed only by methods defined by its class. It is 

through the use of private members that access is controlled.

Restricting access to a class’ members is a fundamental part of object-oriented programming 

because it helps prevent the misuse of an object. By allowing access to private data only 

through a well-defined set of methods, you can prevent improper values from being assigned 

to that data—by performing a range check, for example. It is not possible for code outside 

the class to set the value of a private member directly. You can also control precisely how 

and when the data within an object is used. Thus, when correctly implemented, a class 

creates a “black box” that can be used, but the inner workings of which are not open to 

tampering.


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   228   229   230   231   232   233   234   235   ...   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