C# 0 The Complete Reference


PART I C h a p t e r   1 1



Download 4,07 Mb.
Pdf ko'rish
bet373/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   369   370   371   372   373   374   375   376   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   1 1 :  

I n h e r i t a n c e  

283


PART IPART I

Here is an example of name hiding:

// An example of inheritance-related name hiding.

using System;

class A {

  public int i = 0;

}

// Create a derived class.



class B : A {

  new int i; // this i hides the i in A

  public B(int b) {

    i = b; // i in B

  }

  public void Show() {



    Console.WriteLine("i in derived class: " + i);

  }


}

class NameHiding {

  static void Main() {

    B ob = new B(2);

    ob.Show();

  }


}

First, notice the use of 



new

 in this line.

new int i; // this i hides the i in A

In essence, it tells the compiler that you know a new variable called 



i

 is being created that 

hides the 

i

 in the base class 



A

. If you leave 



new

 out, a warning is generated.

The output produced by this program is shown here:

i in derived class: 2

Since

B

 defines its own instance variable called 



i

, it hides the 



i

 in 


A

. Therefore, when 



Show( )

is invoked on an object of type 



B

, the value of 



i

 as defined by 



B

 is displayed—not the one 

defined in 

A

.

Using base to Access a Hidden Name

There is a second form of 

base

 that acts somewhat like 



this

, except that it always refers to the 

base class of the derived class in which it is used. This usage has the following general form:

base.


member

Here, 


member

 can be either a method or an instance variable. This form of 



base

 is most 

applicable to situations in which member names of a derived class hide members by the 

www.freepdf-books.com




284

 

P a r t   I :  



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

same name in the base class. Consider this version of the class hierarchy from the preceding 

example:

// Using base to overcome name hiding.

using System;

class A {

  public int i = 0;

}

// Create a derived class.



class B : A {

  new int i; // this i hides the i in A

  public B(int a, int b) {

    base.i = a; // this uncovers the i in A

    i = b; // i in B

  }


  public void Show() {

    // This displays the i in A.

    Console.WriteLine("i in base class: " + base.i);

    // This displays the i in B.

    Console.WriteLine("i in derived class: " + i);

  }


}

class UncoverName {

  static void Main() {

    B ob = new B(1, 2);

    ob.Show();

  }


}

This program displays the following:

i in base class: 1

i in derived class: 2

Although the instance variable 

i

 in 


B

 hides the 



i

 in 


A

,

base

 allows access to the 

i

 defined in 

the base class.

Hidden methods can also be called through the use of 



base

. For example, in the 

following code, class 

B

 inherits class 



A

, and both 



A

 and 


B

 declare a method called 



Show( )

.

Inside,



B

’s

Show( )

, the version of 

Show( )

 defined by 



A

 is called through the use of 



base

.

// Call a hidden method.



using System;

class A {

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   369   370   371   372   373   374   375   376   ...   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