Interface là gì C#

Interface là 1 khái niệm thường được thấy trong các ngôn ngữ cấp cao như C#, Java, ... interface mang đến những ràng buộc về hành động cho các đối tượng, vừa mang lại kiến trúc tốt hơn cho hệ thống, tránh sơ sót và dễ dàng bảo trì.

Interfacetrong C# là gì?

Interface gần như là một lớp [class], nhưng chỉ định nghĩa các hành động, khả năng cần thiết như:

  • Khả năng bay: cần các hành động hỗ trợ như cất cánh, bay, hạ cánh.
  • Khả năng kết nối: cần mở kết nối, gửi nhận dữ liệu, xác nhận kết nối, đóng kết nối.
  • Khả năng đọc file: đọc header file, đọc dữ liệu, giải nén file.

Đặc điểm của 1 interface

Interface có các thành viên là methods, properties, indexers, events và không chứa fields.

interfaceIShape{
//methods
voidDraw[];
//properties
double Side { get; set; }
//indexers
double this[int index] { get; set; }
// event
event EventHandler SideChanged;
// field
double m_side; // lỗi: error CS0525: Interfaces cannot contain fields
}

Interface có thể implement nhiều interface cơ sở [base interface], một class hoặc struct có thể implement nhiều interface.

interface IPath { void setSize[]; }
interface IColor { void setBorderColor[]; void setFillColor[]; }
interface IPoint { void setPoint[]; } // interface có thể implement nhiều interface interface IShape : IColor, IPath { void Draw[]; double Side { get; set; } } // class hoặc struct có thể implement nhiều interface class CSquare : IShape, IPoint { private double m_side; public void setSize[] { } public void setPoint[] { } public void setBorderColor[] { } public void setFillColor[] { } public void Draw[] { } public double Side { get { return m_side; } set { m_side = value; } } }

Bất kỳ class hay struct nào implement một interface thì implement tất cả các thành viên và định nghĩa đầy đủ các thành viên của interface đó.

interface IColor { void setBorderColor[]; void setFillColor[]; } class CRectangle : IColor { public void setBorderColor[]{ } // lỗi: error CS0535: 'CRectangle' does not implement interface member 'IColor.setFillColor[]' // thiếu phương thức: setFillColor[] }

Các thành viên của interface không được phép định nghĩa [definition] mà chỉ được khai báo [declaration].

interface IShape { double Side { get; set; } void Draw[] { Console.WriteLine["drawing drawing."]; } // lỗi: error CS0531: 'IShape.Draw[]': interface members cannot have a definition }

Interface không có constructors.

interface IPath { void setSize[]; public IPath[] { Console.WriteLine["Size of path."]; } // lỗi: error CS0526: Interfaces cannot contain constructors }

Nếu một lớp implement từ nhiều interface có cùng tên thành viên thì trong lớp phải chỉ rõ thành viên đó thuộc interface nào [explicit interface].

interface IPath { void setSize[]; } interface IBorder { void setSize[]; } class CCircle : IPath, IBorder { private int m_size; // Bỏ modifier public // nếu có sẽ gây lỗi: error CS0106: The modifier 'public' is not valid for this item // có lỗi vì khi ta sử dụng kiểu định nghĩa đầy đủ . thì compiler sẽ không cần khai báo từ khóa public // explicit interface void IPath.setSize[] { m_size = 3;
Console.WriteLine["path: {0}", m_size]; } // explicit interface void IBorder.setSize[] { m_size = 5; Console.WriteLine["border: {0}", m_size]; } } class Program { static void Main[string[] args] { // Khai báo một thể hiện của lớp CCricle CCircle circle = new CCircle[]; // Khai báo một thể hiện của interface IPath IPath path = [IPath]circle; // Khai báo một thể hiện của interface IBorder IBorder border = [IBorder]circle; // circle.setSize[]; // đoạn code trên gây ra lỗi vì không thể truy cập thành viên explicit interface từ một thể hiện của lớp. // phương thức được gọi thành công từ một thể hiện interface. path.setSize[]; border.setSize[]; Console.ReadKey[]; } }

Output:

path: 3
border: 5

Implement và inherits trong C#

Video liên quan

Chủ Đề