How it is easy to implement and add interchangeable components using interface? Explain with example
using System;
namespace Q178892
{
// Creation of the interface of movement
interface IMovable
{
void Move();
}
// Using an interface in a class Person
class Person : IMovable
{
public void Move()
{
Console.WriteLine("The man is walks");
}
}
// Using an interface in a class Car
class Car : IMovable
{
public void Move()
{
Console.WriteLine("The car is driving");
}
}
// Using an interface in a class Plane
class Plane : IMovable
{
public void Move()
{
Console.WriteLine("The plane is flying");
}
}
class Program
{
static void Main(string[] args)
{
// Instantiating сlass оbjects
Person person = new Person();
Car car = new Car();
Plane plane = new Plane();
// Method call
person.Move();
car.Move();
plane.Move();
// Pause
Console.Read();
}
}
}
Comments
Leave a comment