C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet378/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   374   375   376   377   378   379   380   381   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

ColorTriangle

 can make use of the previously defined classes of 



Triangle

 and 


TwoDShape

, adding only the extra information it needs for its own, specific 

application. This is part of the value of inheritance; it allows the reuse of code.

This example illustrates one other important point: 



base

 always refers to the constructor 

in the closest base class. The 

base

 in 


ColorTriangle

 calls the constructor in 



Triangle

. The 


base

 in 


Triangle

 calls the constructor in 



TwoDShape

. In a class hierarchy, if a base class 

constructor requires parameters, then all derived classes must pass those parameters “up 

the line.” This is true whether or not a derived class needs parameters of its own.



When Are Constructors Called?

In the foregoing discussion of inheritance and class hierarchies, an important question may 

have occurred to you: When a derived class object is created, whose constructor is executed 

first? The one in the derived class or the one defined by the base class? For example, given a 

derived class called 

B

 and a base class called 



A

, is 


A

’s constructor called before 



B

’s, or vice 

versa? The answer is that in a class hierarchy, constructors are called in order of derivation, 

from base class to derived class. Furthermore, this order is the same whether or not 



base

 is 


used. If 

base

 is not used, then the default (parameterless) constructor of each base class will 

be executed. The following program illustrates the order of constructor execution:

// Demonstrate when constructors are called.

using System;

// Create a base class.

class A {

  public A() {

    Console.WriteLine("Constructing A.");

  }


}

// Create a class derived from A.

class B : A {

  public B() {

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   374   375   376   377   378   379   380   381   ...   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