This print for content only-size & color not accurate 7" X 9-1/4" / casebound / malloy



Download 21,46 Mb.
Pdf ko'rish
bet382/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   378   379   380   381   382   383   384   385   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// A simple base class.

class Car

{

public readonly int maxSpeed;



private int currSpeed;

public Car(int max)

{

maxSpeed = max;



}

public Car()

{

maxSpeed = 55;



}

185

C H A P T E R   6

8849CH06.qxd  10/1/07  10:35 AM  Page 185



public int Speed

{

get { return currSpeed; }



set

{

currSpeed += value;



if (currSpeed > maxSpeed)

{

currSpeed = maxSpeed;



}

}

}



}

Notice that the Car class is making use of encapsulation services to control access to the private

currSpeed field using a public property named Speed. At this point you can exercise your Car type as

follows:


static void Main(string[] args)

{

Console.WriteLine("***** Basic Inheritance *****\n");



// Make a Car type.

Car myCar = new Car(80);

myCar.Speed = 50;

Console.WriteLine("My car is going {0} MPH", myCar.Speed);

Console.ReadLine();

}

Specifying a Class Type’s Parent Class

Now assume you wish to build a new class named MiniVan. Like a basic Car, you wish to define the

MiniVan class to support a maximum speed, current speed, and a property named Speed to allow the

object user to modify the object’s state. Clearly, the Car and MiniVan classes are related; in fact we

can say that a MiniVan “is-a” Car. The “is-a” relationship (formally termed classical inheritance)

allows you to build new class definitions that extend the functionality of an existing class.

The existing class that will serve as the basis for the new class is termed a 



base or parent class.

The role of a base class is to define all the common data and members for the classes that extend it.

The extending classes are formally termed derived or child classes. In C#, we make use of the colon

operator on the class definition to establish an “is-a” relationship between classes:




Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   378   379   380   381   382   383   384   385   ...   1830




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