Построить иерархию классов с использованием конструктора - C#

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

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

Как можно построить иерархию классов с использованием конструктора беря в пример Берем 4 класса -Журнал -книга -печатное издание -учебник Начинал в я в таком духе public class PechatnoeIzdaniye { } public class Kniga : PechatnoeIzdaniye { } public class Journal : PechatnoeIzdaniye { } public class Uchebnik : Kniga { } но как бы надо чучуть не так надо что б было как в примере Пример
Листинг программы
  1. class Animal{
  2. public string Name;
  3. private int Weight; protected int Type;
  4. public int Animal(int W, int T, string N){
  5. Weight=W;
  6. Type=T;
  7. Name=N;
  8. }
  9. public int GetWeight(){return Weight;}
  10. }
Находил подобное но не как не могу полностью разобраться если кто сможет добавить Коментарии и разъяснить буду очень благодарен
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Lab_4
  6. {
  7. class Print
  8. {
  9. protected int page = 0;
  10. public void setPage(int page)
  11. {
  12. if (page > 0 && page < 500)
  13. {
  14. this.page = page;
  15. }
  16. else
  17. this.page = 0;
  18. }
  19. public int getAge()
  20. {
  21. return page;
  22. }
  23. protected string name = "";
  24. public void setName(string name)
  25. {
  26. this.name = name;
  27. }
  28. public string getName()
  29. {
  30. return name;
  31. }
  32. }
  33. class Magazine : Print
  34. {
  35. protected int Price = 0;
  36. public void setPrice(int price)
  37. {
  38. if (price > 0 && price < 200)
  39. {
  40. this.Price = price;
  41. }
  42. else
  43. this.Price = 0;
  44. }
  45. public int getPrice()
  46. {
  47. return Price;
  48. }
  49. class Book : Print
  50. {
  51. protected int year = 0;
  52. public void setYear(int year)
  53. {
  54. if (1990 > 0 && year < 2012)
  55. this.year = year;
  56. else
  57. this.year = 0;
  58. }
  59. public int getYear()
  60. {
  61. return year;
  62. }
  63. internal void setPrice(int p)
  64. {
  65. throw new NotImplementedException();
  66. }
  67. }
  68. class Textbook : Book
  69. {
  70. protected int kolvo_statej = 0;
  71. public void setStatya(int kolvo_statej)
  72. {
  73. this.kolvo_statej = kolvo_statej;
  74. }
  75. public int getStatya()
  76. {
  77. return kolvo_statej;
  78. }
  79. }
  80. class Test
  81. {
  82. static void Main(string[] args)
  83. {
  84. Print print = new Print();
  85. Magazine magazine = new Magazine();
  86. Book book = new Book();
  87. Textbook textbook = new Textbook();
  88. Console.WriteLine("введите возможное кол-во страниц печатного издания");
  89. print.setPage(Convert.ToInt32(Console.ReadLine()));
  90. Console.WriteLine("введите имя печатного издания");
  91. print.setName(Convert.ToString(Console.ReadLine()));
  92. Console.WriteLine("_________________________________");
  93. Console.WriteLine("введите кол-во страниц журнала");
  94. magazine.setPage(Convert.ToInt32(Console.ReadLine()));
  95. Console.WriteLine("введите название журнала");
  96. magazine.setName(Convert.ToString(Console.ReadLine()));
  97. Console.WriteLine("введите цену журнала");
  98. magazine.setPrice(Convert.ToInt32(Console.ReadLine()));
  99. Console.WriteLine("_________________________________");
  100. Console.WriteLine("введите кол-во страниц учебника");
  101. textbook.setPage(Convert.ToInt32(Console.ReadLine()));
  102. Console.WriteLine("введите название учебника");
  103. textbook.setName(Convert.ToString(Console.ReadLine()));

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

textual
Листинг программы
  1.     using*System;
  2. using*System.Collections.Generic;
  3. using*System.Linq;
  4. using*System.Text;
  5.  
  6. namespace*Laba223
  7. {
  8. ****public*class*Pechantor_izdanie**//*Печатное*издание*
  9. ****{
  10.  
  11. ********public*string*Name;
  12. ********public*int*col_str;
  13. ********public*int*year;
  14.  
  15. ********public*Pechantor_izdanie*(string*name,*int*col_str,*int*year)
  16. ********{
  17. ************this.Name*=*name;
  18. ************this.col_str*=*col_str;
  19. ************this.year*=*year;
  20. ********}
  21.  
  22. ********public*void*PrintName*(){
  23. ************Console.WriteLine*(""*+*this.Name);
  24. ********}
  25. ********public*void*PrintCol_str*(){
  26. ************Console.WriteLine*(""*+*this.col_str);
  27. ********}
  28. ********public*void*PrintYear*()
  29. ********{
  30.  
  31.  
  32.  
  33. ********
  34. ************Console.WriteLine*(""*+*this.year);
  35. ********}
  36.  
  37. ********//////////////////////////////////////////////////////////////////
  38.  
  39.  
  40. ********public*class*Jurnal*:*Pechantor_izdanie**//*Журнал
  41. ********{
  42. *************
  43. ************public*string*Number;
  44. ************public*Jurnal*(string*name,*int*col_str,*int*year,*string*number)*:*base*(name,*col_str,*year)
  45. ************{
  46. ****************this.Number*=*number;
  47. ************}
  48. ********************
  49.  
  50. ************public*void*PrintNamber*()
  51. ************{
  52.  
  53. ****************Console.WriteLine*(""*+*this.Number);
  54. ************}
  55.  
  56. ************/////////////////////////////////////////////////////////////////
  57.  
  58.  
  59. ************public*class*Book*:*Pechantor_izdanie**//*Книга
  60. ************{
  61. ****************public*string*Janr;
  62.  
  63. ****************public*Book*(string*name,*int*col_str,*int*year,*string*janr)*:*base*(name,*col_str,*year)
  64. ****************{
  65. ********************this.Janr*=*janr;
  66.  
  67. ****************}
  68.  
  69.  
  70. ****************public*void*PrintJanr*()
  71. ****************{
  72.  
  73. ********************Console.WriteLine*(""*+*this.Janr);
  74. ****************}
  75.  
  76. ****************/////////////////////////////////////////////////////////////////////////
  77.  
  78. ****************public*class*Uchebnic*:*Pechantor_izdanie**//*Учебник
  79. ****************{
  80.  
  81. ********************public*string*Autor;
  82.  
  83. ********************public*Uchebnic*(string*name,*int*col_str,*int*year,string*autor):*base*(name,*col_str,*year)
  84.  
  85.  
  86. ********************{
  87. ************************this.Autor*=*autor;
  88. ********************}
  89.  
  90. ********************public*void*PrintAutor*()
  91. ********************{
  92.  
  93. ************************Console.WriteLine*(""*+*this.Autor);
  94. ********************}
  95.  
  96.  
  97. ****************///////////////////////////////////////////////////////////////////////////
  98.  
  99.  
  100.  
  101. ****************}
  102.  
  103.  
  104. ****************public*static*void*Main*(string[]*args)
  105. ****************{
  106. ********************Uchebnic*ns*=*new*Uchebnic*("Buckvar",20,1995,"Pushkin");
  107. ********************Pechantor_izdanie*ps*=*new*Pechantor_izdanie("Buckvar",20,1995);
  108. ********************ps.PrintName();
  109. ********************ps.PrintCol_str*();
  110. ********************ps.PrintYear*();
  111. ********************ns.PrintAutor();
  112. ********************Console.ReadKey*();
  113. ****************}
  114. ************}
  115. ********}
  116. ****}
  117. }

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


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

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

13   голосов , оценка 4.154 из 5

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

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

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