Построить иерархию классов: журнал, книга, печатное издание, учебник - C#

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

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

Помогите пожалуйста... Построить иерархию классов: журнал, книга, печатное издание, учебник. Нашёл пример, но тут не то выводит что нужно..
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Laba223
  6. {
  7. public class Pechantor_izdanie//Печатное издание
  8. {
  9. public string Name;
  10. public int col_str;
  11. public int year;
  12. public Pechantor_izdanie(string name, int col_str, int year)
  13. {
  14. this.Name = name;//обеспечиваем доступ в текущему классу
  15. this.col_str = col_str;
  16. this.year = year;
  17. }
  18. public void PrintName()
  19. {
  20. Console.WriteLine("" + this.Name);
  21. }
  22. public void PrintCol_str()
  23. {
  24. Console.WriteLine("" + this.col_str);
  25. }
  26. public void PrintYear()
  27. {
  28. Console.WriteLine("" + this.year);
  29. }
  30. public class Jurnal : Pechantor_izdanie//Журнал, производный класс
  31. {
  32. public string Number;
  33. public Jurnal(string name, int col_str, int year, string number)
  34. : base(name, col_str, year)
  35. {
  36. this.Number = number;//обеспечиваем доступ в текущему классу
  37. }
  38. public void PrintNamber()
  39. {
  40. Console.WriteLine("" + this.Number);
  41. }
  42. public class Book : Pechantor_izdanie//Книга, производный класс
  43. {
  44. public string Janr;
  45. public Book(string name, int col_str, int year, string janr)
  46. : base(name, col_str, year)
  47. {
  48. this.Janr = janr;//обеспечиваем доступ в текущему классу
  49. }
  50. public void PrintJanr()
  51. {
  52. Console.WriteLine("" + this.Janr);
  53. }
  54. public class Uchebnic : Pechantor_izdanie//Учебник, производный класс
  55. {
  56. public string Autor;
  57. public Uchebnic(string name, int col_str, int year, string autor)
  58. : base(name, col_str, year)//bazovi klas
  59. {
  60. this.Autor = autor;//обеспечиваем доступ в текущему классу
  61. }
  62. public void PrintAutor()
  63. {
  64. Console.WriteLine("" + this.Autor);
  65. } }
  66. public static void Main(string[] args)
  67. {
  68. //sozdanie obekta classa
  69. Uchebnic ns = new Uchebnic("Buckvar", 20, 1995, "Pushkin");
  70. Pechantor_izdanie ps = new Pechantor_izdanie("Buckvar", 20, 1995);
  71. ps.PrintName();
  72. ps.PrintCol_str();
  73. ps.PrintYear();
  74. ns.PrintAutor();
  75. Console.ReadKey();
  76. } } } }}

Решение задачи: «Построить иерархию классов: журнал, книга, печатное издание, учебник»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApplication174
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             var list = new List<Printing>();
  11.  
  12.             list.Add(new Magazine("OutOfMemory", 1245));
  13.             list.Add(new Book("Бесталанный И.И.", "Как забыть таблицу умножения?", 81));
  14.             list.Add(new TextBook(8, "Спейсер Эникей", "C# - что это?", 256));
  15.  
  16.             foreach (var item in list)
  17.                 Console.WriteLine(item);
  18.  
  19.             Console.ReadLine();
  20.         }
  21.     }
  22.  
  23.     abstract class Printing
  24.     {
  25.         public string Title { get; set; }
  26.         public int PageCount { get; set; }
  27.  
  28.         public Printing(string title, int pageCount)
  29.         {
  30.             Title = title;
  31.             PageCount = pageCount;
  32.         }
  33.  
  34.         public override string ToString()
  35.         {
  36.             return "Название: " + Title + ", Страниц: " + PageCount;
  37.         }
  38.     }
  39.  
  40.     class Magazine : Printing
  41.     {
  42.         public Magazine(string title, int pageCount) : base(title, pageCount)
  43.         {
  44.         }
  45.  
  46.         public override string ToString()
  47.         {
  48.             return base.ToString() + ", Журнал";
  49.         }
  50.     }
  51.  
  52.     class Book : Printing
  53.     {
  54.         public string Author { get; set; }
  55.  
  56.         public Book(string author, string title, int pageCount) : base(title, pageCount)
  57.         {
  58.             Author = author;
  59.         }
  60.  
  61.         public override string ToString()
  62.         {
  63.             return base.ToString() + ", Автор: " + Author;
  64.         }
  65.     }
  66.  
  67.     class TextBook : Book
  68.     {
  69.         public int Form { get; set; }//год обучения
  70.  
  71.         public TextBook(int form, string author, string title, int pageCount) : base(author, title, pageCount)
  72.         {
  73.             Form = form;
  74.         }
  75.  
  76.         public override string ToString()
  77.         {
  78.             return base.ToString() + ", Учбеник для " + Form + " класса";
  79.         }
  80.     }
  81. }

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


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

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

9   голосов , оценка 4 из 5

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

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

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