Interfeys toifalari tushunchasi Interfeys toifalarni abstract bazaviy sinflar bilan taqqoslash Maxsus interfeyslarni kiritish
Download 0.77 Mb.
|
MAVZULAR UMUMIY
- Bu sahifa navigatsiya:
- Navbat (FIFO – First in – First out)
Stek (LIFO: Last in – First out)
Stack static void UseGenericStack() { Stack stackOfPeople = new Stack (); stackOfPeople.Push (new Person { FirstName = "Homer", LastName = "Simpson", Age = 47 }); stackOfPeople.Push (new Person { FirstName = "Marge", LastName = "Simpson", Age =45 }); stackOfPeople.Push(new Person { FirstName = "Lisa", LastName = "Simpson", Age = 9 }); // Tepadagi elementni ko’rish, uni chiqarish va yana ko’rish Console.WriteLine("Birinchi xodim: {0}", stackOfPeople.Peek()); Console.WriteLine("Chiqarildi {0}", StackOfPeople.Pop()); Console.WriteLine("\n Birinchi xodim: {0}", stackOfPeople.Peek()); Console.WriteLine("Chiqarildi {0}", stackOfPeople.Pop()); Console.WriteLine("\n Birinchi xodim: {0}", stackOfPeople.Peek()); Console.WriteLine("Chiqarildi {0}", stackOfPeople.Pop()); try { Console.WriteLine("\nn Birinchi xodim: {0}", StackOfPeople.Peek()); Console.WriteLine("Chiqarildi {0}", stackOfPeople.Pop()); } catch (InvalidOperationException ex) { Console.WriteLine("\nError! {0}", ex.Message); // Xato! Stek bo’sh } } Natija: Birinchi xodim: Name: Lisa Simpson, Age: 9 Chiqarildi Name: Lisa Simpson, Age: 9 Birinchi xodim: Name: Marge Simpson, Age: 45 Chiqarildi Name: Marge Simpson, Age: 45 Birinchi xodim: Name: Homer Simpson, Age: 47 Chiqarildi Name: Homer Simpson, Age: 47 Error! Stack empty. Navbat (FIFO – First in – First out) Navbatlar – bu “birinchi kirding – birinchi chiqding” tarzida elementlarga ishlov berish uchun ishlatiladi. Buning uchun Queue Navbatga oid misol ko’rishdan oldin, ushbu statik metod bor (ya’ni yozilgan) deb faraz qilaylik: static void GetCoffee(Person p) { Console.WriteLine("{0} kofe oldi!”, p. FirstName); } Bu metod quyidagi yordamchi metod ichida chaqiriladi: static void UseGenericQueue() { // Uchta odamdan iborat navbat hosil qilish Queue peopleQ = new Queue (); peopleQ.Enqueue(new Person {FirstName= "Homer”, LastName=”Simpson", Age=47}); peopleQ.Enqueue(new Person {FirstName= "Marge", LastName="Simpson", Age=45}); peopleQ.Enqueue(new Person {FirstName= ’’Lisa”, LastName="Simpson", Age=9}); // Navbatda birinchi kim ekanini ko’rish Console.WriteLine("{0} birinchida!", peopleQ.Peek().FirstName); // Hammani navbatdan o’chirish GetCoffee(peopleQ.Dequeue() ) ; GetCoffee(peopleQ.Dequeue()); GetCoffee(peopleQ.Dequeue()); // Navbatdagi kimnidir olishga urinish try { GetCoffee(peopleQ.Dequeue() ) ; } catch(InvalidOperationException e) { Console.WriteLine("Error! {0}", e.Message); // Xato! Navbat bo’sh } } Endi bu metodni Main() metodidan chaqirsak, ushbu natija ekranga chiqariladi: Homer is birinchida! Homer kofe oldi! Marge kofe oldi! Lisa kofe oldi! Error1 Queue empty. Download 0.77 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling