Построить иерархию классов: журнал, книга, печатное издание, учебник - C#
Формулировка задачи:
Помогите пожалуйста... Построить иерархию классов: журнал, книга, печатное издание, учебник. Нашёл пример, но тут не то выводит что нужно..
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Laba223
- {
- public class Pechantor_izdanie//Печатное издание
- {
- public string Name;
- public int col_str;
- public int year;
- public Pechantor_izdanie(string name, int col_str, int year)
- {
- this.Name = name;//обеспечиваем доступ в текущему классу
- this.col_str = col_str;
- this.year = year;
- }
- public void PrintName()
- {
- Console.WriteLine("" + this.Name);
- }
- public void PrintCol_str()
- {
- Console.WriteLine("" + this.col_str);
- }
- public void PrintYear()
- {
- Console.WriteLine("" + this.year);
- }
- public class Jurnal : Pechantor_izdanie//Журнал, производный класс
- {
- public string Number;
- public Jurnal(string name, int col_str, int year, string number)
- : base(name, col_str, year)
- {
- this.Number = number;//обеспечиваем доступ в текущему классу
- }
- public void PrintNamber()
- {
- Console.WriteLine("" + this.Number);
- }
- public class Book : Pechantor_izdanie//Книга, производный класс
- {
- public string Janr;
- public Book(string name, int col_str, int year, string janr)
- : base(name, col_str, year)
- {
- this.Janr = janr;//обеспечиваем доступ в текущему классу
- }
- public void PrintJanr()
- {
- Console.WriteLine("" + this.Janr);
- }
- public class Uchebnic : Pechantor_izdanie//Учебник, производный класс
- {
- public string Autor;
- public Uchebnic(string name, int col_str, int year, string autor)
- : base(name, col_str, year)//bazovi klas
- {
- this.Autor = autor;//обеспечиваем доступ в текущему классу
- }
- public void PrintAutor()
- {
- Console.WriteLine("" + this.Autor);
- } }
- public static void Main(string[] args)
- {
- //sozdanie obekta classa
- Uchebnic ns = new Uchebnic("Buckvar", 20, 1995, "Pushkin");
- Pechantor_izdanie ps = new Pechantor_izdanie("Buckvar", 20, 1995);
- ps.PrintName();
- ps.PrintCol_str();
- ps.PrintYear();
- ns.PrintAutor();
- Console.ReadKey();
- } } } }}
Решение задачи: «Построить иерархию классов: журнал, книга, печатное издание, учебник»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication174
- {
- class Program
- {
- static void Main(string[] args)
- {
- var list = new List<Printing>();
- list.Add(new Magazine("OutOfMemory", 1245));
- list.Add(new Book("Бесталанный И.И.", "Как забыть таблицу умножения?", 81));
- list.Add(new TextBook(8, "Спейсер Эникей", "C# - что это?", 256));
- foreach (var item in list)
- Console.WriteLine(item);
- Console.ReadLine();
- }
- }
- abstract class Printing
- {
- public string Title { get; set; }
- public int PageCount { get; set; }
- public Printing(string title, int pageCount)
- {
- Title = title;
- PageCount = pageCount;
- }
- public override string ToString()
- {
- return "Название: " + Title + ", Страниц: " + PageCount;
- }
- }
- class Magazine : Printing
- {
- public Magazine(string title, int pageCount) : base(title, pageCount)
- {
- }
- public override string ToString()
- {
- return base.ToString() + ", Журнал";
- }
- }
- class Book : Printing
- {
- public string Author { get; set; }
- public Book(string author, string title, int pageCount) : base(title, pageCount)
- {
- Author = author;
- }
- public override string ToString()
- {
- return base.ToString() + ", Автор: " + Author;
- }
- }
- class TextBook : Book
- {
- public int Form { get; set; }//год обучения
- public TextBook(int form, string author, string title, int pageCount) : base(author, title, pageCount)
- {
- Form = form;
- }
- public override string ToString()
- {
- return base.ToString() + ", Учбеник для " + Form + " класса";
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д