C# 0 The Complete Reference



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

A

,

B

, and 

C

,

C

 can be derived 

from 


B

, which can be derived from 



A

. When this type of situation occurs, each derived class 

inherits all of the traits found in all of its base classes. In this case

C

 inherits all aspects of 



B

and


A

.

www.freepdf-books.com




286

 

P a r t   I :  



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

To see how a multilevel hierarchy can be useful, consider the following program. 

In it, the derived class 

Triangle

 is used as a base class to create the derived class called 



ColorTriangle

.

ColorTriangle

 inherits all of the traits of 

Triangle

 and 


TwoDShape

 and 


adds a field called 

color

, which holds the color of the triangle.

// A multilevel hierarchy.

using System;

class TwoDShape {

  double pri_width;

  double pri_height;

  // Default constructor.

  public TwoDShape() {

    Width = Height = 0.0;

  }

  // Constructor for TwoDShape.



  public TwoDShape(double w, double h) {

    Width = w;

    Height = h;

  }


  // Construct object with equal width and height.

  public TwoDShape(double x) {

    Width = Height = x;

  }


  // Properties for Width and Height.

  public double Width {

     get { return pri_width; }

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

  }

  public double Height {



     get { return pri_height; }

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

  }

  public void ShowDim() {



    Console.WriteLine("Width and height are " +

                       Width + " and " + Height);

  }

}

// A derived class of TwoDShape for triangles.



class Triangle : TwoDShape {

  string Style; // private

  /* A default constructor. This invokes the default

     constructor of TwoDShape. */

  public Triangle() {

    Style = "null";

  }

www.freepdf-books.com




Download 4,07 Mb.

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