C# 0 The Complete Reference


PART I C h a p t e r   8



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


PART I

C h a p t e r   8 :  

A   C l o s e r   L o o k   a t   M e t h o d s   a n d   C l a s s e s  

167


PART IPART I

    // Access to alpha and beta is allowed only through methods.

    ob.SetAlpha(-99);

    ob.SetBeta(19);

    Console.WriteLine("ob.alpha is " + ob.GetAlpha());

    Console.WriteLine("ob.beta is " + ob.GetBeta());

    // You cannot access alpha or beta like this:

//  ob.alpha = 10; // Wrong! alpha is private!

//  ob.beta = 9;   // Wrong! beta is private!

    // It is OK to directly access gamma because it is public.

    ob.gamma = 99;

   }


}

As you can see, inside the 



MyClass

 class, 


alpha

is specified as 



private

,

beta

 is private by 

default, and 



gamma

 is specified as 



public

. Because 



alpha

 and 


beta

 are private, they cannot 

be accessed by code outside of their class. Therefore, inside the 

AccessDemo

 class, neither 

can be used directly. Each must be accessed through public methods, such as 

SetAlpha( )

and


GetAlpha( )

. For example, if you were to remove the comment symbol from the 

beginning of the following line

//  ob.alpha = 10; // Wrong! alpha is private!

you would not be able to compile this program because of the access violation. Although 

access to 




Download 4,07 Mb.

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