How it is easy to implement and add interchangeable components using interface? Explain with example
namespace Q178958
{
// 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