Создание интерфейсов транспортных средств - C#
Формулировка задачи:
Создайте три интерфейса, каждый с двумя методами. Наследуйте новый интерфейс от этих трех, добавьте новый метод. Создайте класс, реализующий этот новый интерфейс и так же наследующий от конкретного класса. Теперь напишите четыре метода, каждый из которых получают один из четырех интерфейсов в качестве аргумента. В main( ), создайте объект вашего класса и передайте его каждому из методов. Создайте интерфейс, реализующий список (цвета, созвездия, объекты, ..) и распечатайте их значения в методе main.
Решение задачи: «Создание интерфейсов транспортных средств»
textual
Листинг программы
using System;
namespace ConsoleApplication
{
internal class Program
{
private static void Main()
{
ICrazyFantasyEngineer chimera = new CrazyFantasyEngineer("Vasya", 3, 100.5, 11.3, 2, 10, 9);
PrintVehicle(chimera);
PrintCar(chimera);
PrintTrain(chimera);
PrintCrazyFantasyEngineer(chimera);
}
private static void PrintCar(ICar car)
{
Console.WriteLine("I'm car!");
Console.WriteLine("Body weight: {0}", car.GetBodyWeight());
Console.WriteLine("Tire size: {0}", car.GetTireSize());
Console.WriteLine();
}
private static void PrintCrazyFantasyEngineer(ICrazyFantasyEngineer chimera)
{
Console.WriteLine("My creator hates me!");
Console.WriteLine("Number of unicorns: {0}", chimera.GetNumberOfUnicorns());
Console.WriteLine();
}
private static void PrintTrain(ITrain train)
{
Console.WriteLine("I'm train!");
Console.WriteLine("Number of wagons: {0}", train.GetNumberOfWagons());
Console.WriteLine("Length: {0}", train.GetLength());
Console.WriteLine();
}
private static void PrintVehicle(IVehicle vehicle)
{
Console.WriteLine("I'm vehicle!");
Console.WriteLine("Name: {0}", vehicle.GetName());
Console.WriteLine("Number of passengers: {0}", vehicle.GetNumberOfPassengers());
Console.WriteLine();
}
}
internal interface IVehicle
{
string GetName();
int GetNumberOfPassengers();
}
internal abstract class Vehicle : IVehicle
{
protected Vehicle(string name, int numberOfPassengers)
{
this.Name = name;
this.NumberOfPassengers = numberOfPassengers;
}
protected string Name { get; set; }
protected int NumberOfPassengers { get; set; }
public string GetName()
{
return this.Name;
}
public int GetNumberOfPassengers()
{
return this.NumberOfPassengers;
}
}
internal interface ICar
{
double GetBodyWeight();
double GetTireSize();
}
internal interface ITrain
{
int GetLength();
int GetNumberOfWagons();
}
internal interface ICrazyFantasyEngineer : IVehicle, ICar, ITrain
{
int GetNumberOfUnicorns();
}
internal sealed class CrazyFantasyEngineer : Vehicle, ICrazyFantasyEngineer
{
private readonly double bodyWeight;
private readonly int length;
private readonly int numberOfUnicorns;
private readonly int numberOfWagons;
private readonly double tireSize;
public CrazyFantasyEngineer(string name, int numberOfPassengers,
double bodyWeight, double tireSize, int numberOfWagons, int length, int numberOfUnicorns)
: base(name, numberOfPassengers)
{
this.bodyWeight = bodyWeight;
this.tireSize = tireSize;
this.numberOfWagons = numberOfWagons;
this.length = length;
this.numberOfUnicorns = numberOfUnicorns;
}
public double GetBodyWeight()
{
return this.bodyWeight;
}
public int GetLength()
{
return this.length;
}
public int GetNumberOfUnicorns()
{
return this.numberOfUnicorns;
}
public int GetNumberOfWagons()
{
return this.numberOfWagons;
}
public double GetTireSize()
{
return this.tireSize;
}
}
}