Построить иерархию классов с использованием конструктора - C#
Формулировка задачи:
Как можно построить иерархию классов с использованием конструктора
беря в пример
Берем 4 класса
-Журнал
-книга
-печатное издание
-учебник
Начинал в я в таком духе
public class PechatnoeIzdaniye { }
public class Kniga : PechatnoeIzdaniye { }
public class Journal : PechatnoeIzdaniye { }
public class Uchebnik : Kniga { }
но как бы надо чучуть не так
надо что б было как в примере
Пример
Находил подобное но не как не могу полностью разобраться если кто сможет добавить Коментарии и разъяснить буду очень благодарен
Листинг программы
- class Animal{
- public string Name;
- private int Weight; protected int Type;
- public int Animal(int W, int T, string N){
- Weight=W;
- Type=T;
- Name=N;
- }
- public int GetWeight(){return Weight;}
- }
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Lab_4
- {
- class Print
- {
- protected int page = 0;
- public void setPage(int page)
- {
- if (page > 0 && page < 500)
- {
- this.page = page;
- }
- else
- this.page = 0;
- }
- public int getAge()
- {
- return page;
- }
- protected string name = "";
- public void setName(string name)
- {
- this.name = name;
- }
- public string getName()
- {
- return name;
- }
- }
- class Magazine : Print
- {
- protected int Price = 0;
- public void setPrice(int price)
- {
- if (price > 0 && price < 200)
- {
- this.Price = price;
- }
- else
- this.Price = 0;
- }
- public int getPrice()
- {
- return Price;
- }
- class Book : Print
- {
- protected int year = 0;
- public void setYear(int year)
- {
- if (1990 > 0 && year < 2012)
- this.year = year;
- else
- this.year = 0;
- }
- public int getYear()
- {
- return year;
- }
- internal void setPrice(int p)
- {
- throw new NotImplementedException();
- }
- }
- class Textbook : Book
- {
- protected int kolvo_statej = 0;
- public void setStatya(int kolvo_statej)
- {
- this.kolvo_statej = kolvo_statej;
- }
- public int getStatya()
- {
- return kolvo_statej;
- }
- }
- class Test
- {
- static void Main(string[] args)
- {
- Print print = new Print();
- Magazine magazine = new Magazine();
- Book book = new Book();
- Textbook textbook = new Textbook();
- Console.WriteLine("введите возможное кол-во страниц печатного издания");
- print.setPage(Convert.ToInt32(Console.ReadLine()));
- Console.WriteLine("введите имя печатного издания");
- print.setName(Convert.ToString(Console.ReadLine()));
- Console.WriteLine("_________________________________");
- Console.WriteLine("введите кол-во страниц журнала");
- magazine.setPage(Convert.ToInt32(Console.ReadLine()));
- Console.WriteLine("введите название журнала");
- magazine.setName(Convert.ToString(Console.ReadLine()));
- Console.WriteLine("введите цену журнала");
- magazine.setPrice(Convert.ToInt32(Console.ReadLine()));
- Console.WriteLine("_________________________________");
- Console.WriteLine("введите кол-во страниц учебника");
- textbook.setPage(Convert.ToInt32(Console.ReadLine()));
- Console.WriteLine("введите название учебника");
- textbook.setName(Convert.ToString(Console.ReadLine()));
Решение задачи: «Построить иерархию классов с использованием конструктора»
textual
Листинг программы
- 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)
- ********************{
- ************************this.Autor*=*autor;
- ********************}
- ********************public*void*PrintAutor*()
- ********************{
- ************************Console.WriteLine*(""*+*this.Autor);
- ********************}
- ****************///////////////////////////////////////////////////////////////////////////
- ****************}
- ****************public*static*void*Main*(string[]*args)
- ****************{
- ********************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*();
- ****************}
- ************}
- ********}
- ****}
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д