Thực thi giao diện

7 156 0
Thực thi giao diện

Đang tải... (xem toàn văn)

Thông tin tài liệu

Thực thi giao diện Thực thi giao diện Bởi: Khuyet Danh Một giao diện đưa thay cho lớp trừu tượng để tạo ràng buộc lớp thành phần client Những ràng buộc khai báo cách sử dụng từ khóa interface, từ khóa khai báo kiểu liệu tham chiếu để đóng gói ràng buộc Một giao diện giống lớp chứa phương thức trừu tượng Một lớp trừu tượng dùng làm lớp sở cho họ lớp dẫn xuất từ Trong giao diện trộn lẫn với kế thừa khác Khi lớp thực thi giao diện, lớp phải thực thi tất phương thức giao diện Đây bắt buộc mà lớp phải thực Trong chương thảo luận cách tạo, thực thi sử dụng giao diện Ngoài bàn tới cách thực thi nhiều giao diện với cách kết hợp mở rộng giao diện Và cuối minh họa dùng để kiểm tra lớp thực thi giao diện Thực thi giao diện Cú pháp để định nghĩa giao diện sau: [thuộc tính] [bổ sung truy cập] interface [: danh sách sở] { } Phần thuộc tính đề cập sau Thành phần bổ sung truy cập bao gồm: public, private, protected, internal, protected internal nói đến Chương 4, ý nghĩa tương tự bổ sung truy cập lớp Theo sau từ khóa interface tên giao diện Thông thường tên giao diện bắt đầu với từ I hoa (điều không bắt buộc việc đặt tên rõ ràng dễ hiểu, tránh nhầm lẫn với thành phần khác) Ví dụ số giao diện có tên sau: IStorable, ICloneable, 1/7 Thực thi giao diện Danh sách sở danh sách giao diện mà giao diện mở rộng, phần trình bày phần thực thi nhiều giao diện chương Phần thân giao diện phần thực thi giao diện trình bày bên Giả sử muốn tạo giao diện nhằm mô tả phương thức thuộc tính lớp cần thiết để lưu trữ truy cập từ sở liệu hay thành phần lưu trữ liệu khác tập tin Chúng ta định gọi giao diện IStorage Trong giao diện xác nhận hai phương thức: Read() Write(), khai báo xuất phần thân giao diện sau: interface IStorable { void Read(); void Write(object); } Mục đích giao diện để định nghĩa khả mà muốn có lớp Ví dụ, tạo lớp tên Document, lớp lưu trữ liệu sở liệu, định lớp này thực thi giao diện IStorable Để làm điều này, sử dụng cú pháp giống việc tạo lớp Document thừa kế từ IStorable dùng dấu hai chấm (:) theo sau tên giao diện: public class Document : IStorable { public void Read() { } public void Write() { } } Bây trách nhiệm chúng ta, với vai trò người xây dựng lớp Document phải cung cấp thực thi có ý nghĩa thực cho phương thức giao diện IStorable Chúng ta phải thực thi tất phương thức giao diện, không trình biên dịch báo lỗi Sau đoạn chương trình minh họa việc xây dựng lớp Document thực thi giao diện IStorable Sử dụng giao diện using System; // khai báo giao diện interface IStorable { // giao diện không khai báo bổ sung truy cập // phương thức public không thực thi void Read(); void Write(object obj); int Status { get; set; } } // tạo lớp thực thi giao diện Istorable public class Document : IStorable { public Document( string s) { Console.WriteLine("Creating document with: {0}", s); } // thực thi phương thức Read() public void Read() { 2/7 Thực thi giao diện Console.WriteLine("Implement the Read Method for IStorable"); } // thực thi phương thức Write public void Write( object o) { Console.WriteLine("Impleting the Write Method for IStorable"); } // thực thi thuộc tính public int Status { get { return status; } set { status=value; } } // lưu trữ giá trị thuộc tính private int status = 0; } public class Tester { static void Main() { // truy cập phương thức đối tượng Document Document doc = new Document("Test Document"); doc.Status = -1; doc.Read(); Console.WriteLine("Document Status: {0}", doc.Status); // gán cho giao diện sử dụng giao diện IStorable isDoc = (IStorable) doc; isDoc.Status = 0; isDoc.Read(); Console.WriteLine("IStorable Status: {0}", isDoc.Status); } } Kết quả: Creating document with: Test Document Implementing the Read Method for IStorable Document Status: -1 Implementing the Read Method for IStorable IStorable Status: Ví dụ định nghĩa giao diện IStorable với hai phương thức Read(), Write() thuộc tính tên Status có kiểu số nguyên Lưu ý phần khai báo thuộc tính phần thực thi cho get() set() mà đơn giản khai báo có hành vi get() set(): int Status { get; set;} Ngoài phần định nghĩa phương thức giao diện phần bổ sung truy cập (ví dụ như: public, protected, internal, private) Việc cung cấp bổ sung truy cập tạo lỗi Những phương thức giao diện ngầm định public giao diện ràng buộc sử dụng lớp khác Chúng ta tạo thể giao diện, thay vào tạo thể lớp có thực thi giao diện Một lớp thực thi giao diện phải đáp ứng đầy đủ xác ràng buộc khai báo giao diện Lớp Document phải cung cấp hai phương thức Read() Write() 3/7 Thực thi giao diện với thuộc tính Status Tuy nhiên cách thực yêu cầu hoàn toàn phụ thuộc vào lớp Document Mặc dù IStorage lớp Document phải có thuộc tính Status hay không quan tâm đến việc lớp Document lưu trữ trạng thái thật biến thành viên, hay việc tìm kiếm sở liệu Những chi tiết phụ thuộc vào phần thực thi lớp Thực thi nhiều giao diện Trong ngôn ngữ C# cho phép thực thi nhiều giao diện Ví dụ, lớp Document lưu trữ liệu nén Chúng ta chọn thực thi hai giao diện IStorable ICompressible Như phải thay đổi phần khai báo danh sách sở để hai giao diện điều thực thi, sử dụng dấu phẩy (,) để phân cách hai giao diện: public class Document : IStorable, Icompressible Do Document phải thực thi phương thức xác nhận giao diện ICompressible: public void Compress() { Console.WriteLine("Implementing the Compress Method"); } public void Decompress() { Console.WriteLine("Implementing the Decompress Method"); } Bổ sung thêm phần khai báo giao diện ICompressible định nghĩa phương thức giao diện bên lớp Document Sau tạo thể lớp Document gọi phương thức từ giao diện ta có kết tương tự sau: Creating document with: Test Document Implementing the Read Method for IStorable Implementing Compress Mở rộng giao diện C# cung cấp chức cho mở rộng giao diện có cách thêm phương thức thành viên hay bổ sung cách làm việc cho thành viên Ví dụ, mở rộng giao diện ICompressible với giao diện ILoggedCompressible Giao diện mở rộng giao diện cũ cách thêm phương thức ghi log liệu lưu: interface ILoggedCompressible : ICompressible { void LogSavedBytes(); } Các lớp khác thực thi tự giao diện ICompressible hay ILoggedCompressible tùy thuộc vào mục đích có cần thêm chức hay không Nếu lớp thực thi 4/7 Thực thi giao diện giao diện ILoggedCompressible, lớp phải thực thi tất phương thức hai giao diện ICompressible giao diện ILoggedCompressible Những đối tượng lớp thực thi giao diện ILoggedCompressible gán cho hai giao diện ILoggedCompressible ICompressible Kết hợp giao diện Một cách tương tự, tạo giao diện cách kết hợp giao diện cũ ta thêm phương thức hay thuộc tính cho giao diện Ví dụ, định tạo giao diện IStorableCompressible Giao diện kết hợp phương thức hai giao diện thêm vào phương thức để lưu trữ kích thước nguyên thuỷ liệu trước nén: interface IStorableCompressible ILoggedCompressible : IStoreable, { void LogOriginalSize(); } Minh họa việc mở rộng kết hợp giao diện using System; interface IStorable { void Read(); void Write(object obj); int Status { get; set;} } // giao diện interface ICompressible { void Compress(); void Decompress(); } // mở rộng giao diện interface ILoggedCompressible : ICompressible { void LogSavedBytes(); } // kết hợp giao diện interface IStorableCompressible : IStorable, ILoggedCompressible { void LogOriginalSize(); } interface IEncryptable { void Encrypt(); void Decrypt(); } public class Document : IStorableCompressible, Iencryptable { // khởi tạo lớp Document lấy tham số public Document( string s) { Console.WriteLine("Creating document with: {0}", s); } // thực thi giao diện IStorable public void Read() { Console.WriteLine("Implementing the Read Method for IStorable"); } public void Write( object o) { Console.WriteLine("Implementing the Write Method for 5/7 Thực thi giao diện IStorable"); } public int Status { get { return status; } set { status=value; } } // thực thi ICompressible public void Compress() { Console.WriteLine("Implementing Compress"); } public void Decompress() { Console.WriteLine("Implementing Decompress"); } // thực thi giao diện IloggedCompressible public void LogSavedBytes() { Console.WriteLine("Implementing LogSavedBytes"); } // thực thi giao diện IStorableCompressible public void LogOriginalSize() { Console.WriteLine("Implementing LogOriginalSize"); } // thực thi giao diện public void Encrypt() { Console.WriteLine("Implementing Encrypt"); } public void Decrypt() { Console.WriteLine("Implementing Decrypt"); } // biến thành viên lưu liệu cho thuộc tính private int status = 0; } public class Tester { public static void Main() { // tạo đối tượng document Document doc = new Document("Test Document"); // gán đối tượng cho giao diện IStorable isDoc = doc as IStorable; if ( isDoc != null) { isDoc.Read(); } else { Console.WriteLine("IStorable not supported"); } ICompressible icDoc = doc as ICompressible; if ( icDoc != null ) { icDoc.Compress(); } else { Console.WriteLine("Compressible not supported"); } ILoggedCompressible ilcDoc = doc as ILoggedCompressible; if ( ilcDoc != null ) { ilcDoc.LogSavedBytes(); ilcDoc.Compress(); // ilcDoc.Read(); // gọi } else { Console.WriteLine("LoggedCompressible not supported"); } IStorableCompressible isc = doc as IStorableCompressible; if ( isc != null ) { isc.LogOriginalSize(); // IStorableCompressible isc.LogSavedBytes();// ILoggedCompressible isc.Compress(); // ICompress isc.Read();// IStorable } else { Console.WriteLine("StorableCompressible not supported"); } IEncryptable ie = doc as IEncryptable; if ( ie != null ) { ie.Encrypt(); } else { Console.WriteLine("Encryptable not supported"); } } } Kết quả: 6/7 Thực thi giao diện Creating document with: Test Document Implementing the Read Method for IStorable Implementing Compress Implementing LogSavedBytes Implementing Compress Implementing LogOriginalSize Implementing LogSaveBytes Implementing Compress Implementing the Read Method for IStorable Implementing Encrypt Ví dụ bắt đầu việc thực thi giao diện IStorable giao diện ICompressible Sau phần mở rộng đến giao diện ILoggedCompressible sau kết hợp hai vào giao diện IStorableCompressible Và giao diện cuối ví dụ IEncrypt Chương trình Tester tạo đối tượng Document sau gán vào giao diện khác Khi đối tượng gán cho giao diện ILoggedCompressible, dùng giao diện để gọi phương thức giao diện ICompressible ILogged- Compressible mở rộng thừa kế phương thức từ giao diện sở: ILoggedCompressible ilcDoc = doc as ILoggedCompressible; if ( ilcDoc != null ) { ilcDoc.LogSavedBytes(); ilcDoc.Compress(); // ilcDoc.Read(); // gọi } Tuy nhiên, gọi phương thức Read() phương thức giao diện IStorable, không liên quan đến giao diện Nếu thêm lệnh vào chương trình biên dịch lỗi Nếu gán vào giao diện IStorableCompressible, giao diện kết hợp hai giao diện IStorable giao diện ICompressible, gọi tất phương thức IStorableCompressible, ICompressible, IStorable: IStorableCompressible isc = doc as IStorableCompressible; if ( isc != null ) { isc.LogOriginalSize();// IStorableCompressible isc.LogSaveBytes(); // ILoggedCompressible isc.Compress(); // ICompress isc.Read();// IStorable } 7/7 .. .Thực thi giao diện Danh sách sở danh sách giao diện mà giao diện mở rộng, phần trình bày phần thực thi nhiều giao diện chương Phần thân giao diện phần thực thi giao diện trình bày... lớp thực thi 4/7 Thực thi giao diện giao diện ILoggedCompressible, lớp phải thực thi tất phương thức hai giao diện ICompressible giao diện ILoggedCompressible Những đối tượng lớp thực thi giao diện. .. phụ thuộc vào phần thực thi lớp Thực thi nhiều giao diện Trong ngôn ngữ C# cho phép thực thi nhiều giao diện Ví dụ, lớp Document lưu trữ liệu nén Chúng ta chọn thực thi hai giao diện IStorable ICompressible

Ngày đăng: 31/12/2015, 21:47

Từ khóa liên quan

Mục lục

  • Thực thi giao diện

  • Thực thi một giao diện

  • Thực thi nhiều giao diện

    • Mở rộng giao diện

    • Kết hợp các giao diện

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan