C# 0 The Complete Reference


TwoDShape , it can access  Width



Download 4,07 Mb.
Pdf ko'rish
bet358/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   354   355   356   357   358   359   360   361   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

TwoDShape

, it can access 



Width

 and 


Height

 inside 


Area( )

. Also, inside 



Main( )

, objects 



t1

 and 


t2

 can refer to 



Width

and


Height

 directly, as if they were part of 



Triangle

. Figure 11-1 depicts conceptually how 



TwoDShape

 is incorporated into 



Triangle

.

Even though 



TwoDShape

 is a base for 



Triangle

, it is also a completely independent, 

stand-alone class. Being a base class for a derived class does not mean that the base class 

cannot be used by itself. For example, the following is perfectly valid:

TwoDShape shape = new TwoDShape();

shape.Width = 10;

shape.Height = 20;

shape.ShowDim();

Of course, an object of 

TwoDShape

 has no knowledge of or access to any classes derived 

from 

TwoDShape

.

W



H

A

F



IGURE

 11-1 


A conceptual depiction of the 

Triangle


 class

www.freepdf-books.com




272

 

P a r t   I :  



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

The general form of a 



class

 declaration that inherits a base class is shown here:

class

derived-class-name

 : 


base-class-name

 {

   // body of class



}

You can specify only one base class for any derived class that you create. C# does not 

support the inheritance of multiple base classes into a single derived class. (This differs 

from C++, in which you can inherit multiple base classes. Be aware of this when converting 

C++ code to C#.) You can, however, create a hierarchy of inheritance in which a derived 

class becomes a base class of another derived class. (Of course, no class can be a base class 

of itself, either directly or indirectly.) In all cases, a derived class inherits all of the members 

of its base class. This includes instance variables, methods, properties, and indexers.

A major advantage of inheritance is that once you have created a base class that defines 

the attributes common to a set of objects, it can be used to create any number of more 

specific derived classes. Each derived class can precisely tailor its own classification. For 

example, here is another class derived from 



TwoDShape

 that encapsulates rectangles:

// A derived class of TwoDShape for rectangles.

class Rectangle : TwoDShape {

  // Return true if the rectangle is square.

  public bool IsSquare() {

    if(Width == Height) return true;

    return false;

  }

  // Return area of the rectangle.



  public double Area() {

    return Width * Height;

  }

}

The




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   354   355   356   357   358   359   360   361   ...   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