[关闭]
@sharif 2018-02-26T11:59:23.000000Z 字数 2523 阅读 936

第十一章习题

习题

知识点一

集合CollectionBase
键控集合DictionaryBase

知识点二

迭代器
IEnumerable 与 IEnumerator
迭代一个类,可以使用方法GetEnumerator(),返回类型IEnumerator;
迭代一个类成员,可以使用方法GetEnumerable(),返回类型IEnumerable

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Exercise
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. Person p1 = new Person("1", 10);
  15. Person p2 = new Person("2", 89);
  16. Person p3 = new Person("3", 58);
  17. Person p4 = new Person("4", 49);
  18. People people = new People();
  19. people.Add(p1);
  20. people.Add(p2);
  21. people.Add(p3);
  22. people.Add(p4);
  23. foreach (int i in people.GetAges())
  24. {
  25. Console.WriteLine($"i:{i}");
  26. }
  27. Console.ReadKey();
  28. }
  29. }
  30. public class Person
  31. {
  32. private string name;
  33. private int age;
  34. public string Name { get => name; set => name = value; }
  35. public int Age { get => age; set => age = value; }
  36. public Person(string name, int age)
  37. {
  38. this.age = age;
  39. this.name = name;
  40. }
  41. public Person()
  42. {
  43. }
  44. public static bool operator >(Person per1, Person per2) => per1.age > per2.age;
  45. public static bool operator <(Person per1, Person per2) => per1.age > per2.age;
  46. public static bool operator >=(Person per1, Person per2) => !(per1 < per2);
  47. public static bool operator <=(Person per1, Person per2) => !(per1 > per2);
  48. }
  49. public class People : DictionaryBase, ICloneable
  50. {
  51. public void Add(Person newPerson)
  52. {
  53. Dictionary.Add(newPerson.Name, newPerson);
  54. }
  55. public void Remove(string personName)
  56. {
  57. Dictionary.Remove(personName);
  58. }
  59. public Person this[string personName]
  60. {
  61. get { return (Person)Dictionary[personName]; }
  62. set { Dictionary[personName] = value; }
  63. }
  64. public Person[] GetOldest()
  65. {
  66. Person oldestPerson = null;
  67. People oldestPeople = new People();
  68. Person currentPerson;
  69. foreach (DictionaryEntry p in Dictionary)
  70. {
  71. currentPerson = p.Value as Person;
  72. if (oldestPerson == null)
  73. {
  74. oldestPerson = currentPerson;
  75. }
  76. else
  77. {
  78. if (currentPerson > oldestPerson)
  79. {
  80. oldestPeople.Clear();
  81. oldestPeople.Add(currentPerson);
  82. }
  83. else
  84. {
  85. if (currentPerson >= oldestPerson)//没有定义‘==’运算哦
  86. {
  87. oldestPeople.Add(currentPerson);
  88. }
  89. }
  90. }
  91. }
  92. Person[] returnPersons = new Person[Dictionary.Count];
  93. for (int i = 0; i < Dictionary.Count; i++)
  94. {
  95. returnPersons[i] = (Person)Dictionary[i];
  96. }
  97. Dictionary.CopyTo(returnPersons, 0);
  98. return returnPersons;
  99. }
  100. public object Clone()
  101. {
  102. //People newPeople = new People();
  103. //foreach (Person per in List)
  104. //{
  105. // newPeople.List.Add(new Person(per.Name, per.Age));
  106. //}
  107. //return (object)newPeople;
  108. People clonePeople = new People();
  109. Person currentPerson, newPerson;
  110. foreach (DictionaryEntry p in Dictionary)
  111. {
  112. currentPerson = p.Value as Person;
  113. newPerson = new Person();
  114. newPerson.Name = currentPerson.Name;
  115. newPerson.Age = currentPerson.Age;
  116. clonePeople.Add(newPerson);
  117. }
  118. return clonePeople;
  119. }
  120. public IEnumerable GetAges()
  121. {
  122. foreach (Object per in Dictionary)
  123. {
  124. yield return (per as Person).Age;
  125. }
  126. }
  127. }
  128. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注