Interfeys toifalari tushunchasi Interfeys toifalarni abstract bazaviy sinflar bilan taqqoslash Maxsus interfeyslarni kiritish


BinaryOp Ь2 = new BinaryOp(SimpleMath.SquareNumber)


Download 0.77 Mb.
bet34/50
Sana26.01.2023
Hajmi0.77 Mb.
#1129439
1   ...   30   31   32   33   34   35   36   37   ...   50
Bog'liq
MAVZULAR UMUMIY

BinaryOp Ь2 = new BinaryOp(SimpleMath.SquareNumber);

Buning sababi yangi metodda 2 ta emas, bitta argument borligidadir, biz e’lon qilgan BinaryOp delegati esa 2 argumentlidir.


Endi Program sinfiga ushbuni qo’shaylik:
static void DisplayDelegatelnfo(Delegate delObj)
{
// Delegat chaqiruvi ro’yxatidagi barcha ismlarni chiqarish
foreach (Delegate d in delObj.GetInvocationList ())
{
Console.WriteLine("Method Name: {0}", d.Method);
// metod nomi
Console.WriteLine("Type Name: {0}", d.Target);
// toifa nomi
}
}
Endi agar Main() ga yangi metodni chaqirish qo’shilgan bo’lsa
BinaryOp b = new BinaryOp(SimpleMath.Add);
DisplayDelegatelnfо (b);

natija ushbu bo’ladi:
***** Oddiy delegatga misol *****
Method Name: Int32 Add(Int32, Int32)
Type Name:
10 + 10 is 20

Murakkabroq misol

  1. Chaqiruvchi kodga xabarlarni yuboruvchi yangi toifa delegatni e’lon qilish

  2. Car sinfida shu delegat toifasiga mansub o’zgaruvchini e’lon qilish

  3. Car sinfida chaqiruvchi kodga teskari chaqiruv metodini ko’rsatuvchi yordamchi funktsiya yaratish

  4. Tegishli holatlarda delegat chaqiruvlari ro’yxatiga murojaat qilish uchun Accelerate () metodini yozish

Avval Car sinfini yaratish:
public class Car
{
// Holat ma’lumotlari
public int CurrentSpeed { get; set; }
public int MaxSpeed { get; set; } = 100;
public string PetName { get; set; }
// Avtomobil sozmi?
private bool carlsDead;
// Sinf konstruktorlari
public Car () {}
public Car(string name, int maxSp, int currSp)
{
CurrentSpeed = currSp;
MaxSpeed = maxSp;
PetName = name;
}
}
Endi Car ga qo’shimchalar kiritish:
public class Car

// 1. Delegat toifasini kiritish
public delegate void CarEngineHandler(string msgForCaller);
//2. Shu toifa o’zgaruvchisi e’loni
private CarEngineHandler listOfHandlers;
// 3. Chaqiruvchi kod uchun registratsiya funktsiyasini yozish (qo’shish)
public void RegisterWithCarEngine(CarEngineHandler methodToCall)
{
listOfHandlers = methodToCall;
}
}
Endi 4-ish:
// 4. Tegishli holatlarda delegat chaqiruvlari ro’yxatiga murojaat
// qilish uchun Accelerate () metodini yozish
public void Accelerate(int delta)
{
// Agar avtomobil ishdan chiqqan bo’lsa, bu haqda xabar yuborish
if (carlsDead)
{
if (listOfHandlers != null)
listOfHandlers("Uzr, avto o’ldi...");
else
CurrentSpeed += delta;
// Avtomobil deyarli ishdan chiqdimi?
if (10 == (MaxSpeed - CurrentSpeed)
&& listOfHandlers != null)
{
listOfHandlers("Ehtiyot bo’lmoq kerak!");
}
if (CurrentSpeed >= MaxSpeed)
carlsDead = true;
else
Console .WnteLine ( "CurrentSpeed = {0}", CurrentSpeed);
}
}

Asosiy qism, ya’ni Main() ko’rinishi:


class Program
{
static void Main(string [ ] args)
{
Console.WriteLine("***** Delegatlar xodisa imkonlari sifatida *****\n");
// Car ob’yektini yaratish
Car cl = new Car ("SlugBug", 100, 10) ;
// Car ob’yektiga xabar yubormoqchi bo’lganida qaysi metodni
// chaqirishni bildirish
cl.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
// Tezlikni oshirish ( xodisani keltirib chiqarish uchun).
Console.WriteLine ( "***** Tezlik oshmoqda *****");
for (int i = 0; i < 6; i + + )
cl.Accelerate(20);
Console.ReadLine();
}
// Kirish xabarlari uchun maqsad
Console.WriteLine("\п***** Car ob’yektidan xabar *****");
Console.WriteLine ( "=> {0}", msg);
Console.WriteLine("*********************************\n") ;
}
}

NATIJA:
***** Delegatlar xodisa imkonlari sifatida *****


***** Tezlik oshmoqda *****
CurrentSpeed = 30
CurrentSpeed = 50
CurrentSpeed = 70
***** Car ob’yektidan xabar *****
=> Ehtiyot bo’lmoq kerak!
*********************************
CurrentSpeed = 90
***** Car ob’yektidan xabar *****
=> Uzr, avto o’ldi...
*********************************


Download 0.77 Mb.

Do'stlaringiz bilan baham:
1   ...   30   31   32   33   34   35   36   37   ...   50




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling