C# 0 The Complete Reference


virtual  and  abstract



Download 4,07 Mb.
Pdf ko'rish
bet394/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   390   391   392   393   394   395   396   397   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

virtual

 and 


abstract

 together.

To declare an abstract method, use this general form:

abstract


type name

(

parameter-list

);

As you can see, no method body is present. The 



abstract

 modifier can be used only on 

instance methods. It cannot be applied to 

static

 methods. Properties and indexers can also 

be abstract.

www.freepdf-books.com




302

 

P a r t   I :  



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

A class that contains one or more abstract methods must also be declared as abstract by 

preceding its 

class

 declaration with the 



abstract

 specifier. Since an abstract class does not 

define a complete implementation, there can be no objects of an abstract class. Thus, attempting 

to create an object of an abstract class by using 



new

 will result in a compile-time error.

When a derived class inherits an abstract class, it must implement all of the abstract 

methods in the base class. If it doesn’t, then the derived class must also be specified as 



abstract

. Thus, the 



abstract

 attribute is inherited until such time as a complete implementation 

is achieved.

Using an abstract class, you can improve the 



TwoDShape

 class. Since there is no meaningful 

concept of area for an undefined two-dimensional figure, the following version of the preceding 

program declares 



Area( )

 as 


abstract

 inside 


TwoDShape

 and 


TwoDShape

 as 


abstract

. This, 


of course, means that all classes derived from 

TwoDShape

 must override 



Area( )

.

// Create an abstract class.



using System;

abstract class TwoDShape {

  double pri_width;

  double pri_height;

  // A default constructor.

  public TwoDShape() {

    Width = Height = 0.0;

    name = "null";

  }

  // Parameterized constructor.



  public TwoDShape(double w, double h, string n) {

    Width = w;

    Height = h;

    name = n;

  }

  // Construct object with equal width and height.



  public TwoDShape(double x, string n) {

    Width = Height = x;

    name = n;

  }


  // Construct a copy of a TwoDShape object.

  public TwoDShape(TwoDShape ob) {

    Width = ob.Width;

    Height = ob.Height;

    name = ob.name;

  }


  // Properties for Width and Height.

  public double Width {

     get { return pri_width; }

     set { pri_width = value < 0 ? -value : value; }

  }

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   390   391   392   393   394   395   396   397   ...   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