Lektion 12: lösningsförslag

  1. // 1
  2. interface IPerson
  3. {
  4. string Name { get; set; }
  5. int Age { get; set; }
  6. string Summary { get; }
  7. }
  8. class Student1 : IPerson
  9. {
  10. public string Name { get; set; }
  11. public int Age { get; set; }
  12. public double Points { get; set; }
  13. public string Summary {
  14. get
  15. {
  16. return Name + " is " + Age + " years old.";
  17. }
  18. }
  19. }
  20. class Teacher1 : IPerson
  21. {
  22. public string Name { get; set; }
  23. public int Age { get; set; }
  24. public double HourlySalary { get; set; }
  25. public string Summary
  26. {
  27. get
  28. {
  29. return Name + " is " + Age + " years old.";
  30. }
  31. }
  32. }
  33. class Administrator1 : IPerson
  34. {
  35. public string Name { get; set; }
  36. public int Age { get; set; }
  37. public double MonthlySalary { get; set; }
  38. public string Summary
  39. {
  40. get
  41. {
  42. return Name + " is " + Age + " years old.";
  43. }
  44. }
  45. }
  46. static void ShowPeople1(IPerson[] people)
  47. {
  48. foreach (IPerson p in people)
  49. {
  50. Console.WriteLine(p.Summary);
  51. }
  52. }
  53. class Person
  54. {
  55. public string Name { get; set; }
  56. public int Age { get; set; }
  57. public string Summary
  58. {
  59. get
  60. {
  61. return Name + " is " + Age + " years old.";
  62. }
  63. }
  64. }
  65. class Student2 : Person
  66. {
  67. public double Points { get; set; }
  68. }
  69. class Teacher2 : Person
  70. {
  71. public double HourlySalary { get; set; }
  72. }
  73. class Administrator2 : Person
  74. {
  75. public double MonthlySalary { get; set; }
  76. }
  77. static void ShowPeople2(Person[] people)
  78. {
  79. foreach (Person p in people)
  80. {
  81. Console.WriteLine(p.Summary);
  82. }
  83. }
  84. static void Main(string[] args)
  85. {
  86. IPerson[] people1 = new IPerson[]
  87. {
  88. new Teacher1 {Name="Jakob", Age=30,HourlySalary=999},
  89. new Administrator1 {Name="Jakob", Age=31,MonthlySalary=12345},
  90. new Student1 {Name="Jakob", Age=32,Points=20},
  91. };
  92. ShowPeople1(people1);
  93. Person[] people2 = new Person[]
  94. {
  95. new Teacher2 {Name="Jakob", Age=30,HourlySalary=999},
  96. new Administrator2 {Name="Jakob", Age=31,MonthlySalary=12345},
  97. new Student2 {Name="Jakob", Age=32,Points=20},
  98. };
  99. ShowPeople2(people2);
  100. }
  101. // 2
  102. class MyForm : Form
  103. {
  104. private int ClickCount = 0;
  105. public MyForm()
  106. {
  107. Button button = new Button
  108. {
  109. Text = "Click Me!"
  110. };
  111. Controls.Add(button);
  112. button.Click += OnButtonClick;
  113. }
  114. private void OnButtonClick(object sender, EventArgs e)
  115. {
  116. ClickCount += 1;
  117. MessageBox.Show("Times clicked: " + ClickCount);
  118. }
  119. }