Lập trình C# - Xây dựng Interface Drivable 

Viết chương trình C# thực hiện các công việc sau:

  • Tạo giao diện IDrivable với hai phương thức Start và Stop và thuộc tính chỉ đọc Started.
  • Tạo giao diện ISteerable với hai phương thức TurnLeftTurnRight
  • Tạo giao diện IMovable kế thừa từ 2 giao diện trên bổ sung thêm hai phương thức Accelerate và Brake
  • Tạo lớp Car thực thi từ giao diện IMovable và code cho tất cả các phương thức có thể
  • Trong Main code để test lớp Car.

Bước 1: Kích chuột phải vào Solution  chọn Add -> New Project ->Nhập tên(InterfaceDrivable) -> Chọn OK.

Bước 2: Tạo nhiều lớp giao diện IDrivable, IMovable, ISteerable và code theo gợi ý sau:

1. Code giao diện IDrivable:

using System;
using System.Collections.Generic;
using System.Text;

namespace InterfaceDrivable
{
    interface IDrivable
    {
        void Start();
        void Stop();
        bool Started
        {
            get;
        }
    }
}

2. Code giao diện IMovable:

using System;
using System.Collections.Generic;
using System.Text;

namespace InterfaceDrivable
{
    interface IMovable : IDrivable, ISteerable
    {
        void Accelerate();
        void Brake();
    }
}

3. Code giao diện ISteerable:

using System;
using System.Collections.Generic;
using System.Text;

namespace InterfaceDrivable
{
    interface ISteerable
    {
        void TurnRight();
        void TurnLeft();
    }
}

Bước 3: Tạo lớp Car và code theo gợi ý sau:

using System;
using System.Collections.Generic;
using System.Text;

namespace InterfaceDrivable
{
    class Car : IMovable
    {

        bool started = true;
        bool IDrivable.Started
        {
            get
            {
                return started;
            }
        }     

        public void Start()
        {
            Console.WriteLine("Car started");
            started = true;
        }

        public void Stop()
        {
            Console.WriteLine("Car stopped");
            started = false;
        }
        public void TurnLeft()
        {
            Console.WriteLine("Car turning left");
        }

        public void TurnRight()
        {
            Console.WriteLine("Car turning right");
        }
        public void Accelerate()
        {
            Console.WriteLine("Car Accelerating");
        }

        public void Brake()
        {
            Console.WriteLine("Car braking");
        }       
       
    }
}

Bước 4Trong Program code test như sau:

using System;

namespace InterfaceDrivable
{
    class Program
    {
        static void Main(string[] args)
        {
            Car _Car = new Car();
            Console.WriteLine("calling _Car.Start()");
            _Car.Start();
            Console.WriteLine("calling _Car.TurnLeft()");
            _Car.TurnLeft();
            Console.WriteLine("calling _Car.Accelerate()");
            _Car.Accelerate();
            Console.ReadKey();
        }
    }
}

Xem ví dụ

Bước 5: Nhấn Ctrl+F5 để chạy và xem kết quả

calling _Car.Start()
Car started
calling _Car.TurnLeft()
Car turning left
calling _Car.Accelerate()
Car Accelerating