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

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

  • Tạo giao diện IPerson với các phương thức InsertDeleteUpdateDisplay.
  • Tạo lớp Staff thực thi các phương thức trong giao diện IPerson.
  • Tạo lớp Student với các trường id, name, age và thực thi các phương thức trong giao diện IPerson.
  • Trong Main code để test.

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

Bước 2Tạo lớp giao diện IPerson và code theo gợi ý sau:
Thực hiện tạo các phương thức thêm (Insert), Sửa (Update), Xóa (Delete) và hiển thị (Display)

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

namespace InterfacePerson
{
    interface IPerson
    {
        void Insert(Object obj);
        void Delete(Object obj);
        void Update(Object obj);
        void Display(Object obj);
    }
}

Bước 3Tạo lớp Staff  là lớp triển khai lớp giao diện IPerson và code theo gợi ý sau:

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

namespace InterfacePerson
{
    class Staff : IPerson
    {
        public void Insert(object obj)
        {
            Console.WriteLine("Đã lưu đối tượng:" + obj);
        }
        public void Update(object obj)
        {
            Console.WriteLine("Đã cập nhật đối tượng:" + obj);
        }
        public void Delete(object obj)
        {
            Console.WriteLine("Đã xóa đối tượng:" + obj);
        }

        public void Display(object obj)
        {
            Console.WriteLine("Thông tin đối tượng:" + obj);
        }      
       
    }
}

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

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

namespace InterfacePerson
{
    class Student : IPerson
    {
        public string Code { get; set; }

        public string Name { get; set; }
        public int Age { get; set; }

        public void Insert(object obj)
        {
            Console.WriteLine("Đã lưu đối tượng:" + obj);
        }
        public void Update(object obj)
        {
            Console.WriteLine("Đã cập nhật đối tượng:" + obj);
        }
        public void Delete(object obj)
        {
            Console.WriteLine("Đã xóa đối tượng:" + obj);
        }
        public void Display(object obj)
        {
            Student std = (Student)obj;
            Console.WriteLine("Mã sinh viên: {0}", std.Code);
            Console.WriteLine("Tên sinh viên: {0}", std.Name);
            Console.WriteLine("Tuổi sinh viên: {0}", std.Age);
        }

        
    }
}

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

using System;
using System.Text;

namespace InterfacePerson
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            IPerson _Staff = new Staff();
            Object data = "Hiepsiit";
            _Staff.Insert(data);
            _Staff.Update(data);
            _Staff.Delete(data);
            _Staff.Display(data);
            Console.WriteLine("-------------------------------------------");
            IPerson __Student = new Student() { Code="988TC777722",Name="Thích Qua Môn",Age=20};
            __Student.Display(__Student);
            Console.ReadKey();
        }
    }
}
Xem ví dụ

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

Đã lưu đối tượng:Hiepsiit
Đã cập nhật đối tượng:Hiepsiit
Đã xóa đối tượng:Hiepsiit
Thông tin đối tượng:Hiepsiit
-------------------------------------------
Mã sinh viên: 988TC777722
Tên sinh viên: Thích Qua Môn
Tuổi sinh viên: 20