Как реализовать интерфейс IComparable в индексируемом классе - C#

Узнай цену своей работы

Формулировка задачи:

Как реализовать интерфейс IComparable, если невозможно доступиться до индексируемых полей внутри самого индексируемого класса.
Листинг программы
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. string[] abit = new string[]
  11. {
  12. "Udachin", "Nigamaeva","Seregin","Miloradob","Postnikova","Gorobchenko","Moroz","Nosova","Ananiyn","Kulagina","Muhin","Starikova","Borodin"
  13. };
  14. Journal journ = new Journal();
  15. for (int i = 0; i < abit.Length; i++)
  16. {
  17. journ[i] = new Student(i + 1, abit[i]);
  18. }
  19. foreach (Student st in journ)
  20. Console.WriteLine(st.Number + " => " + st.Name);
  21. Console.ReadLine();
  22. }
  23. }
  24. class Student
  25. {
  26. int number;
  27. string name;
  28. public Student(int number, string name)
  29. {
  30. this.number = number;
  31. this.name = name;
  32. }
  33. public int Number
  34. {
  35. get { return number; }
  36. set
  37. {
  38. if (value > 0 || value < 31)
  39. number = value;
  40. }
  41. }
  42. public string Name
  43. {
  44. get { return name; }
  45. set
  46. {
  47. if (value.Length > 0 && value.Length < 51)
  48. name = value;
  49. }
  50. }
  51. }
  52. class Journal: IEnumerable, IComparable
  53. {
  54. ArrayList journal = new ArrayList();
  55. public Student this[int index]
  56. {
  57. get { return (Student)journal[index]; }
  58. set
  59. {
  60. if (index > -1 && (value is Student))
  61. {
  62. journal.Add(value);
  63. }
  64. }
  65. }
  66. public int Length
  67. {
  68. get { return journal.Count;}
  69. }
  70. public IEnumerator GetEnumerator()
  71. {
  72. foreach (object o in journal)
  73. yield return o;
  74. }
  75. public int CompareTo(object input) //???
  76. {
  77. return 1;
  78. }
  79. }

Решение задачи: «Как реализовать интерфейс IComparable в индексируемом классе»

textual
Листинг программы
  1. class Student : IComparable<Student>
  2. {
  3.     int number;
  4.     string name;
  5.  
  6.     public Student(int number, string name)
  7.     {
  8.         this.number = number;
  9.         this.name = name;
  10.     }
  11.  
  12.     public int Number
  13.     {
  14.         get { return number; }
  15.         set
  16.         {
  17.             if (value > 0 || value < 31)
  18.                 number = value;
  19.         }
  20.     }
  21.  
  22.     public string Name
  23.     {
  24.         get { return name; }
  25.         set
  26.         {
  27.             if (value.Length > 0 && value.Length < 51)
  28.                 name = value;
  29.         }
  30.     }
  31.    
  32.     public int CompareTo(Student other)
  33.     {
  34.         if ((object)other == null) return 1;
  35.         return string.Compare(this.Name, other.Name);
  36.     }
  37. }
  38.  
  39. class Journal : IEnumerable<Student>
  40. {
  41.     List<Student> journal = new List<Student>();
  42.  
  43.     public void Add(Student student)
  44.     {
  45.         journal.Add(student);
  46.         journal.Sort();
  47.     }
  48.    
  49.     public Student this[int index]
  50.     {
  51.         get { return journal[index]; }
  52.         set { journal[index] = value; }
  53.     }
  54.  
  55.     public int Length
  56.     {
  57.         get { return journal.Count; }
  58.     }
  59.  
  60.     public IEnumerator<Student> GetEnumerator()
  61.     {
  62.         return journal.GetEnumerator();
  63.     }
  64.    
  65.     IEnumerator IEnumerable.GetEnumerator()
  66.     {
  67.         return GetEnumerator();
  68.     }
  69. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 3.818 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы