C# 0 The Complete Reference


protected  access modifier. When a member  of a class is declared as  protected



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

protected

 access modifier. When a member 

of a class is declared as 

protected

, that member is, with one important exception, private. 

The exception occurs when a protected member is inherited. In this case, a protected member 

of the base class becomes a protected member of the derived class and is, therefore, accessible 

to the derived class. Therefore, by using 

protected

, you can create class members that are 

private to their class but that can still be inherited and accessed by a derived class.

Here is a simple example that uses 



protected

:

// Demonstrate protected.



using System;

class B {

  protected int i, j; // private to B, but accessible by D

  public void Set(int a, int b) {

    i = a;

    j = b;

  }

  public void Show() {



    Console.WriteLine(i + " " + j);

 }

}



www.freepdf-books.com


276

 

P a r t   I :  



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

class D : B {

  int k; // private

  // D can access B's i and j

  public void Setk() {

     k = i * j;

  }

  public void Showk() {



    Console.WriteLine(k);

  }


}

class ProtectedDemo {

  static void Main() {

    D ob = new D();

    ob.Set(2, 3); // OK, known to D

    ob.Show();    // OK, known to D

    ob.Setk();  // OK, part of D

    ob.Showk(); // OK, part of D

  }

}

In this example, because 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   361   362   363   364   365   366   367   368   ...   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