.NET 4.x Работа с классами - C#

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

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

Добавьте два класса Person и Staff - (это сделал, работает). Создайте два класса Teacher и Developer, производные от Staff. Для класса Teaсher добавьте поле subject, а для класса Developer - поле level, переопределите метод Print для обоих классов. - тут чтото неполучается...как сделать?
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Home_Work_7
  7. {
  8. public class Person
  9. {
  10. private string name;
  11. public Person(string name)
  12. {
  13. this.name = name;
  14. }
  15. virtual public string Name
  16. {
  17. get
  18. {
  19. return name;
  20. }
  21. }
  22. virtual public void Print()
  23. {
  24. Console.WriteLine("Name: {0}", this.name);
  25. }
  26. public class Staff : Person
  27. {
  28. private int salary;
  29. public Staff(string name, int salary): base(name)
  30. {
  31. this.salary = salary;
  32. }
  33. override public string Name
  34. {
  35. get
  36. {
  37. return base.Name + "Staff";
  38. }
  39. }
  40. override public void Print()
  41. {
  42. Console.WriteLine("Person {0} has salary: ${1}", Name, this.salary);
  43. }
  44.  
  45. public class Teacher : Staff
  46. {
  47. }
  48. public class Developer : Staff
  49. {
  50. }
  51. }
  52. }
  53. public class Program
  54. {
  55. static void Main(string[] args)
  56. {
  57. List<Person> people = new List<Person>();
  58. people.Add(new Person("Yura"));
  59. people.Add(new Person.Staff("Ira ", 300));
  60. foreach (var p in people)
  61. p.Print();
  62. Console.ReadLine();
  63. }
  64. }
  65. }

Решение задачи: «.NET 4.x Работа с классами»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Home_Work_7
  8. {
  9.     public class Person
  10.     {
  11.         private string name;
  12.         public Person(string name)
  13.         {
  14.             this.name = name;
  15.         }
  16.         virtual public string Name
  17.         {
  18.             get
  19.             {
  20.                 return name;
  21.             }
  22.         }
  23.         virtual public void Print()
  24.         {
  25.             Console.WriteLine("Name: {0}", this.name);
  26.         }
  27.  
  28.  
  29.     }
  30.  
  31.     public class Staff : Person
  32.     {
  33.         private int salary;
  34.         public Staff(string name, int salary)
  35.             : base(name)
  36.         {
  37.             this.salary = salary;
  38.         }
  39.  
  40.         override public string Name
  41.         {
  42.             get
  43.             {
  44.                 return base.Name + "Staff";
  45.             }
  46.         }
  47.         override public void Print()
  48.         {
  49.             Console.WriteLine("Person {0} has salary: ${1}", Name, this.salary);
  50.         }
  51.        
  52.  
  53.         public class Teacher : Staff
  54.         {
  55.             private string subject;
  56.             public Teacher(string name, string subject, int salary)
  57.                 : base(name, salary)
  58.             {
  59.  
  60.                 this.subject = subject;
  61.             }
  62.             override public string Name
  63.             {
  64.                 get
  65.                 {
  66.                     return base.Name + "Teacher";
  67.                 }
  68.             }
  69.             override public void Print()
  70.             {
  71.                 Console.WriteLine("Teacher - {0}, subject: {1} has salary: ${2}", Name, this.subject, salary);
  72.             }
  73.         }
  74.  
  75.         public class Developer : Staff
  76.         {
  77.             private string level;
  78.             public Developer(string name, string level, int salary)
  79.                 : base(name, salary)
  80.             {
  81.  
  82.                 this.level = level;
  83.             }
  84.             override public string Name
  85.             {
  86.                 get
  87.                 {
  88.                     return base.Name + "Developer";
  89.                 }
  90.             }
  91.             override public void Print()
  92.             {
  93.                 Console.WriteLine("Developer - {0}, level - {1} has salary: ${2} ", Name, this.level, salary);
  94.             }
  95.         }
  96.     }
  97.  
  98.  
  99.     public class Program
  100.     {
  101.         static void Main(string[] args)
  102.         {
  103.             List<Person> people = new List<Person>();
  104.             people.Add(new Person("Yura "));
  105.             people.Add(new Staff(" Ira ", 300));
  106.             people.Add(new Staff.Teacher(" Jaroslav Garasym ", "C# ", 500));
  107.             people.Add(new Staff.Developer(" Ivan ", " Profi ", 1000));
  108.             foreach (var p in people)
  109.             {
  110.                 p.Print();
  111.             }
  112.             Console.WriteLine("Enter name: ");
  113.             string str = Console.ReadLine();
  114.             foreach (var p in people)
  115.             {
  116.                 if (p.Name == str)
  117.                 {
  118.                    p.Print();
  119.                 }              
  120.             }
  121.             Console.ReadLine();
  122.         }
  123.     }
  124. }

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


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

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

10   голосов , оценка 3.9 из 5

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

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

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