Algoritm pdf
IEnumerable va IEnumerator
Download 292.61 Kb. Pdf ko'rish
|
algoritm
- Bu sahifa navigatsiya:
- IEnumerator obekt
IEnumerable va IEnumerator dasturini amalga oshirish
Misol yordamida eng oddiy IEnumerable dasturini ko'rib chiqing: using System.Collections; Week week = new Week(); foreach (var day in week) { Console.WriteLine(day); } class Week : IEnumerable { string[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; public IEnumerator GetEnumerator() => days.GetEnumerator(); } Bunday holda, haftani ifodalovchi va haftaning barcha kunlarini saqlaydigan hafta sinfi IEnumerable interfeysini amalga oshiradi. Biroq, bu holda biz juda sodda ish qildik - IEnumerator dasturini amalga oshirish o'rniga, biz faqat getenumerator usulida massiv uchun IEnumerator ob'ektini qaytaramiz. public IEnumerator GetEnumerator() => days.GetEnumerator(); Buning yordamida biz haftaning barcha kunlarini foreach tsiklida takrorlashimiz mumkin. Shu bilan birga, shuni ta'kidlash kerakki, foreach orqali to'plamni qidirish uchun, asosan, IEnumerable interfeysini amalga oshirish shart emas. Sinfda IEnumerator ob'ektini qaytaradigan umumiy getenumerator usulini aniqlash kifoya. Masalan: class Week { string[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; public IEnumerator GetEnumerator() =>days.GetEnumerator(); } Biroq, bu juda oddiy edi - biz faqat tayyor massiv sanoqchisidan foydalanamiz. Biroq, siz o'zingizning ob'ektlarni ro'yxatga olish mantig'ini o'rnatishingiz kerak bo'lishi mumkin. Buning uchun IEnumerator interfeysini amalga oshiramiz: using System.Collections; class WeekEnumerator : IEnumerator { string[] days; int position = -1; public WeekEnumerator(string[] days) => this.days = days; public object Current { get { if (position == -1 || position >= days.Length) throw new ArgumentException(); return days[position]; } } public bool MoveNext() { if (position < days.Length - 1) { position++; return true; } else return false; } public void Reset() => position = -1; } class Week { string[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; public IEnumerator GetEnumerator() => new WeekEnumerator(days); } Bu erda endi Week klassi o'rnatilgan sanoqchidan emas, balki IEnumerator-ni amalga oshiradigan Weakenumerator-dan foydalanadi. Numeratorni amalga oshirishda asosiy nuqta ko'rsatgichni elementga o'tkazishdir. Weakenumerator sinfida joriy pozitsiyani saqlash uchun pozitsiya o'zgaruvchisi aniqlanadi. Shuni yodda tutish kerakki, eng boshida (boshlang'ich holatda) ko'rsatgich birinchi elementdan oldin shartli ravishda pozitsiyani ko'rsatishi kerak. Foreach tsikli amalga oshirilganda, ushbu tsikl avval MoveNext usulini chaqiradi va aslida ko'rsatgichni bitta pozitsiyaga oldinga siljitadi va shundan keyingina joriy pozitsiyada elementni olish uchun joriy xususiyatga murojaat qiladi. Keyin dasturda biz foreach loopi yordamida hafta ob'ektini xuddi shunday takrorlashimiz mumkin: Week week = new Week(); foreach(var day in week) |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling