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



Download 21,46 Mb.
Pdf ko'rish
bet456/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   452   453   454   455   456   457   458   459   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Constant for maximum speed.

public const int MaxSpeed = 100;



// Internal state data.

private int currSpeed;

private string petName;

// Is the car still operational?

private bool carIsDead;



// A car has-a radio.

private Radio theMusicBox = new Radio();



// Constructors.

public Car() {}

public Car(string name, int currSp)

{

currSpeed = currSp;



petName = name;

}

public void CrankTunes(bool state)



{

// Delegate request to inner object.

theMusicBox.TurnOn(state);

}

// See if Car has overheated.

public void Accelerate(int delta)

{

if (carIsDead)



Console.WriteLine("{0} is out of order...", petName);

else


{

currSpeed += delta;

if (currSpeed > MaxSpeed)

{

Console.WriteLine("{0} has overheated!", petName);



currSpeed = 0;

C H A P T E R   7  



U N D E R S TA N D I N G   S T R U C T U R E D   E X C E P T I O N   H A N D L I N G



223

8849CH07.qxd  10/1/07  10:38 AM  Page 223




carIsDead = true;

}

else



Console.WriteLine("=> CurrSpeed = {0}", currSpeed);

}

}



}

Now, if we were to implement a Main() method that forces a Car object to exceed the prede-

fined maximum speed as shown here:

static void Main(string[] args)

{

Console.WriteLine("***** Simple Exception Example *****");



Console.WriteLine("=> Creating a car and stepping on it!");

Car myCar = new Car("Zippy", 20);

myCar.CrankTunes(true);

for (int i = 0; i < 10; i++)

myCar.Accelerate(10);

Console.ReadLine();

}

we would see the output displayed in Figure 7-1.



Figure 7-1.

The Car in action


Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   452   453   454   455   456   457   458   459   ...   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