C# 0 The Complete Reference


base ) to this  TwoDShape



Download 4,07 Mb.
Pdf ko'rish
bet386/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   382   383   384   385   386   387   388   389   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

base

) to this 



TwoDShape

 constructor:

public TwoDShape(TwoDShape ob) {

  Width = ob.Width;

  Height = ob.Height;

}

www.freepdf-books.com




294

 

P a r t   I :  



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

The key point is that 



TwoDShape( )

 is expecting a 



TwoDShape

 object. However, 



Triangle( )

 passes it a 



Triangle

 object. As explained, the reason this works is because a base 

class reference can refer to a derived class object. Thus, it is perfectly acceptable to pass 

TwoDShape( )

 a reference to an object of a class derived from 



TwoDShape

. Because the 



TwoDShape( )

 constructor is initializing only those portions of the derived class object 

that are members of 

TwoDShape

, it doesn’t matter that the object might also contain other 

members added by derived classes.

Virtual Methods and Overriding

A

virtual method

 is a method that is declared as 

virtual

 in a base class. The defining 

characteristic of a virtual method is that it can be redefined in one or more derived classes. 

Thus, each derived class can have its own version of a virtual method. Virtual methods are 

interesting because of what happens when one is called through a base class reference. In 

this situation, C# determines which version of the method to call based upon the 



type

 of the 


object

referred to

 by the reference—and this determination is made 



at runtime.

 Thus, when 

different objects are referred to, different versions of the virtual method are executed. In 

other words, it is the type of the object being referred to (not the type of the reference) that 

determines which version of the virtual method will be executed. Therefore, if a base class 

contains a virtual method and classes are derived from that base class, then when different 

types of objects are referred to through a base class reference, different versions of the 

virtual method are executed.

You declare a method as virtual inside a base class by preceding its declaration with the 

keyword 


virtual

. When a virtual method is redefined by a derived class, the 



override

 modifier

is used. Thus, the process of redefining a virtual method inside a derived class is called 

method

overriding.

 When overriding a method, the name, return type, and signature of the overriding 

method must be the same as the virtual method that is being overridden. Also, a virtual 

method cannot be specified as 



static

 or 


abstract

 (discussed later in this chapter).

Method overriding forms the basis for one of C#’s most powerful concepts: 

dynamic

method dispatch.

 Dynamic method dispatch is the mechanism by which a call to an 

overridden method is resolved at runtime, rather than compile time. Dynamic method 

dispatch is important because this is how C# implements runtime polymorphism.

Here is an example that illustrates virtual methods and overriding:

// Demonstrate a virtual method.

using System;

class Base {

  // Create virtual method in the base class.

  public virtual void Who() {

    Console.WriteLine("Who() in Base");

  }


}

class Derived1 : Base {

  // Override Who() in a derived class.

  public override void Who() {

    Console.WriteLine("Who() in Derived1");

  }


}

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   382   383   384   385   386   387   388   389   ...   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