Transport class:
abstract class Transport //: IComparable<Transport> { protected string stamp;//марка protected int num;//номер protected int speed;//скорость public float carry;//грузоподъёмность public abstract void Show(); public abstract void Carrying(); /* public int CompareTo(Transport obj) { if (this.carry > obj.carry) return 1; if (this.carry < obj.carry) return -1; else return 0; }*/ } Car class:
class Passenger_car : Transport { public Passenger_car(string stamp,int num,int speed,float carry) { this.stamp = stamp; this.num = num; this.speed = speed; this.carry = carry; } public override void Show() { Console.WriteLine("Марка-{0}\tНомер-{1}\tСкорость{2}\tГрузоподъёмность{3}", stamp, num, speed, carry); } public override void Carrying() { Console.WriteLine("Грузоподъёмность = {0}",carry); } } Motorcycle class:
class Motorcycle : Passenger_car { public bool lulka; public Motorcycle(string stamp, int num, int speed, float carry,bool lulka):base(stamp,num,speed,carry) { this.lulka = lulka; if (this.lulka == false) { base.carry = 0; } } public override void Show() { Console.WriteLine("Марка-{0}\tНомер-{1}\tСкорость{2}\tГрузоподъёмность{3}\tЕсть люлька?->{4}", stamp, num, speed, carry, lulka); } public override void Carrying() { Console.WriteLine("Грузоподъёмность = {0}", carry); } } Truck class:
class Truck : Passenger_car { protected bool trailer; public Truck(string stamp, int num, int speed, float carry,bool trailer):base(stamp, num, speed, carry) { this.trailer = trailer; if (this.trailer == true) { base.carry = carry * 2; } } public override void Show() { Console.WriteLine("Марка-{0}\tНомер-{1}\tСкорость{2}\tГрузоподъёмность{3}\tЕсть прицеп?->{4}", stamp, num, speed, carry, trailer); } public override void Carrying() { Console.WriteLine("Грузоподъёмность = {0}", carry); } } Main class:
class Program { static void Main() { List <Transport> n= new List<Transport>(); n.Add ( new Passenger_car("BMW",777,500,200)); n.Add(new Motorcycle("KAWASAKI", 666, 350, 100,false)); n.Add (new Truck("KAMAZ",555,60,1000,true)); //n.Sort(); foreach (Transport item in n) { item.Show(); item.Carrying(); Console.WriteLine(); } } }
Show,Carrying)? Instead of theShowmethod, it is better to override the standardToString, the second method is also better to rewrite so that it returns a string. So your classes will be more flexible and you can use them in other types of projects. - user227049