C# 0 The Complete Reference


alpha  by code outside of  MyClass



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

alpha

 by code outside of 



MyClass

 is not allowed, methods defined within 



MyClass

 can freely access it, as the 



SetAlpha( )

 and 


GetAlpha( )

 methods show. The same 

is true for 

beta

.

The key point is this: A private member can be used freely by other members of its class, 



but it cannot be accessed by code outside its class.

Applying Public and Private Access

The proper use of public and private access is a key component of successful object-oriented 

programming. Although there are no hard and fast rules, here are some general principles 

that serve as guidelines:

•  Members of a class that are used only within the class itself should be private.

•  Instance data that must be within a specific range should be private, with access 

provided through public methods that can perform range checks.

•  If changing a member can cause an effect that extends beyond the member itself 

(that is, affects other aspects of the object), that member should be private, and 

access to it should be controlled.

•  Members that can cause harm to an object when improperly used should be private. 

Access to these members should be through public methods that prevent improper 

usage.

•  Methods that get and set the values of private data must be public.



•  Public instance variables are permissible when there is no reason for them to be 

private.


www.freepdf-books.com


168

 

P a r t   I :  



T h e   C #   L a n g u a g e

Of course, there are many nuances that the preceding rules do not address, and special 

cases cause one or more rules to be violated. But, in general, if you follow these rules, you 

will be creating resilient objects that are not easily misused.




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   231   232   233   234   235   236   237   238   ...   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