Advertisement
Avele

Явный интерфейс

May 15th, 2021 (edited)
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Shapes
  4. {
  5.     class Person
  6.     {
  7.         string name;
  8.         int age;
  9.         DateTime data { get; set; }
  10.         public Person(string n, DateTime d, int a)
  11.         {
  12.             name = n;
  13.             age = a;
  14.             data = d;
  15.         }
  16.         public override string ToString()
  17.         {
  18.             return String.Format("{0} {1} {2}", data.Date.ToString("d"), name, age);
  19.         }
  20.     }
  21.     interface Iind
  22.     {
  23.         PersonList ReverseList();
  24.         int Count { get; }
  25.         Person this[int i] { get; }
  26.     }
  27.     class PersonList : Iind
  28.     {
  29.         Person[] M;
  30.         int count;
  31.         public PersonList(Person[] l)
  32.         {
  33.             M = l;
  34.             count = l.Length;
  35.         }
  36. ОН РЕАЛИЗУЕТСЯ НАУХЙ
  37. Не слушайте ЕГО
  38. Интерфейс нельзя наследовать
  39.         public Person this[int i]
  40.         {
  41.             get
  42.             {
  43.                 return M[i];
  44.             }
  45.         }
  46.  
  47.         Person Iind.this[int i] => this.M[i];
  48.  
  49.         int Iind.Count => this.count;
  50.  
  51.  
  52.         PersonList Iind.ReverseList()
  53.         {
  54.             Person[] n = new Person[count];
  55.             for (int i = 0; i < count; i++)
  56.                 n[i] = M[count - 1 - i];
  57.             return new PersonList(n);
  58.         }
  59.     }
  60.     class Program
  61.     {
  62.         static void Main(string[] args)
  63.         {
  64.             Person p1 = new Person("Ivan", new DateTime(2001, 12, 24), 16);
  65.             Person p2 = new Person("Vasya", new DateTime(2000, 8, 23), 12);
  66.             Iind personList = new PersonList(new Person[] { p1, p2 });
  67.             for (int i = 0; i < personList.Count; i++)
  68.                 Console.WriteLine(personList[i]);
  69.             for (int i = 0; i < personList.Count; i++)
  70.                 Console.WriteLine(personList.ReverseList()[i]);
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement