Нужно реализовать интерфейс IComparable в каждом из наследуемых классов - C#
Формулировка задачи:
Вот у меня есть абстрактный класс и наследуемые от него три, запихнул их в коллекцию. Нужно реализовать интерфейс IComparable в каждом из наследуемых классов, помогите , а ?
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace OOP_Lab_11 { abstract class Software { public string Name; public string Manufacturer; public virtual void ShowInfo() { Console.WriteLine("Name: " + Name); Console.WriteLine("Manufacturer: " + Manufacturer); } public abstract void Search(string n); } class Freeware : Software { public Freeware(string Name, string Manufacturer) { this.Name = Name; this.Manufacturer = Manufacturer; } public override void ShowInfo() { Console.WriteLine("Freeware: "); base.ShowInfo(); } public override void Search(string n) { } } class ShareWare : Software { protected string TimeOfFreeUsing; protected string Date; public ShareWare(string Name, string Manufacturer, string Date, string TimeOfFreeUsing) { this.Name = Name; this.Manufacturer = Manufacturer; this.TimeOfFreeUsing = TimeOfFreeUsing; this.Date = Date; } public override void ShowInfo() { Console.WriteLine(); Console.WriteLine("ShareWare: "); base.ShowInfo(); Console.WriteLine("Date of installing: " + Date); Console.WriteLine("Time of free using: " + TimeOfFreeUsing); } public override void Search(string n) { if (this is ShareWare) if (n == this.Date) this.ShowInfo(); } } class Commercial : Software { public int Price; protected string Date; protected string TimeOfUsing; public Commercial(string Name, string Manufacturer, int Price, string Date, string TimeOfUsing) { this.Name = Name; this.Manufacturer = Manufacturer; this.Price = Price; this.TimeOfUsing = TimeOfUsing; this.Date = Date; } public override void ShowInfo() { Console.WriteLine(); Console.WriteLine("Commercial: "); base.ShowInfo(); Console.WriteLine("Price: " + Price); Console.WriteLine("Date of installing: " +Date); Console.WriteLine("Time of using: " +TimeOfUsing); } public override void Search(string n) { if (this is Commercial) if (n == this.Date) this.ShowInfo(); } } class Program { private static List<Freeware> FreewareCollection; private static List<ShareWare> SharewareCollection; private static List<Commercial> CommercialCollection; static void Main(string[] args) { FreewareCollection = new List<Freeware>(); SharewareCollection = new List<ShareWare>(); CommercialCollection = new List<Commercial>(); Console.WriteLine("Enter the number of list elements"); int n = Convert.ToInt16(Console.ReadLine()); for (int i = 0; i < n; i++) { Console.WriteLine("Freeware: "); Console.Write("Name: "); string FrwNm = Console.ReadLine(); Console.Write("Manufacturer: "); string FrwMnf = Console.ReadLine(); Freeware Frw = new Freeware(FrwNm, FrwMnf); FreewareCollection.Add(Frw); /*-----------------------------------------*/ Console.WriteLine("ShareWare: "); Console.WriteLine("Name: "); string ShrwNm = Console.ReadLine(); Console.WriteLine("Manufacturer: "); string ShrwMnf = Console.ReadLine(); Console.WriteLine("Date of installing: "); string ShrwDateOf = Console.ReadLine(); Console.WriteLine("Time of free using: "); string ShrwTmOf = Console.ReadLine(); ShareWare Shrw = new ShareWare(ShrwNm, ShrwMnf, ShrwDateOf, ShrwTmOf); SharewareCollection.Add(Shrw); /*-----------------------------------------*/ Console.WriteLine("Commercial: "); Console.WriteLine("Name: "); string ComNm = Console.ReadLine(); Console.WriteLine("Manufacturer: "); string ComMnf = Console.ReadLine(); Console.WriteLine("Price: "); int ComPrice = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Date of installing: "); string ComDateOf = Console.ReadLine(); Console.WriteLine("Time of Using: "); string ComTimeOf = Console.ReadLine(); Commercial Comm = new Commercial(ComNm, ComMnf, ComPrice, ComDateOf, ComTimeOf); CommercialCollection.Add(Comm); /*-----------------------------------------*/ } Console.WriteLine("Collection output"); for (int i = 0; i < n; i++) { FreewareCollection[i].ShowInfo(); SharewareCollection[i].ShowInfo(); CommercialCollection[i].ShowInfo(); } CommercialCollection.Sort(); Console.WriteLine("For exit press enter"); Console.ReadLine(); } } }
Решение задачи: «Нужно реализовать интерфейс IComparable в каждом из наследуемых классов»
textual
Листинг программы
class Freeware : Software, IComparable { public Freeware(string Name, string Manufacturer) { this.Name = Name; this.Manufacturer = Manufacturer; } public override void ShowInfo() { Console.WriteLine("Freeware: "); base.ShowInfo(); } public override void Search(string n) { } public int CompareTo(object obj) { var freeware = (Freeware)obj; return Name.CompareTo(freeware.Name); } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д