Danh mục

THỰC THI GIAO DIỆN phần 2

Số trang: 18      Loại file: pdf      Dung lượng: 190.03 KB      Lượt xem: 13      Lượt tải: 0    
Jamona

Hỗ trợ phí lưu trữ khi tải xuống: 7,000 VND Tải xuống file đầy đủ (18 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Bổ sung thêm phần khai báo giao diện ICompressible và định nghĩa các phương thức của giao diện bên trong lớp Document. Sau khi tạo thể hiện lớp Document và gọi các phương thức từ giao diện ta có kết quả tương tự như sau: Creating document with: Test Document Implementing the Read Method for IStorable Implementing
Nội dung trích xuất từ tài liệu:
THỰC THI GIAO DIỆN phần 2Do đó Document cũng phải thực thi những phương thức được xác nhận trong giaodiệnICompressible: 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 và định nghĩa các phươngthức của giao diện bên trong lớp Document. Sau khi tạo thể hiện lớp Document vàgọi các phương thức từ giao diện ta có kết quả tương tự như sau:Creating document with: Test DocumentImplementing the Read Method forIStorable Implementing CompressMở rộng giao diện C# cung cấp chức năng cho chúng ta mở rộng một giao diện đã có bằng cách thêmcác phương thức và các thành viên hay bổ sung cách làm việc cho các thành viên. Ví dụ,chúng tacó thể mở rộng giao diện ICompressible với một giao diện mới làILoggedCompressible. Giao diện mới này mở rộng giao diện cũ bằng cách thêm phươngthức ghi log các dữ liệu đãlưu: interface ILoggedCompressible : ICompressible { void LogSavedBytes(); }Các lớp khác có thể thực thi tự do giao diện ICompressible hayILoggedCompressible tùy thuộc vào mục đích có cần thêm chức năng hay không.Nếu một lớp thực thi giao diện ILoggedCompressible, thì lớp này phải thực thi tấtcả các phương thức của cả hai giao diện ICompressible và giao diệnILoggedCompressible. Những đối tượng của lớp thực thi giao diệnILoggedCompressible có thể được gán cho cả hai giao diện ILoggedCompressiblevà ICompressible.Kết hợp các giao diện Một cách tương tự, chúng ta có thể tạo giao diện mới bằng cách kết hợp các giao diệncũ và ta có thể thêm các phương thức hay các thuộc tính cho giao diện mới. Ví dụ,chúng ta quyết định tạo một giao diện IStorableCompressible. Giao diện mới này sẽkết hợp những phương thức của cả hai giao diện và cũng thêm vào một phương thứcmới để lưu trữ kích thước nguyên thuỷ của các dữ liệu trước khi nén: interface IStorableCompressible : IStoreable, ILoggedCompressible { void LogOriginalSize(); } Ví dụ 8.2: Minh họa việc mở rộng và kết hợp các giao diện.-----------------------------------------------------------------------------using System;interface IStorable{ void Read(); void Write(object obj); int Status { get; set;}}// giao diện mớiinterfaceICompressible{ void Compress(); void Decompress();}// mở rộng giao diệninterface ILoggedCompressible : ICompressible{ void LogSavedBytes();}// kết hợp giao diệninterface IStorableCompressible : IStorable, ILoggedCompressible{ void LogOriginalSize();}interface IEncryptable{ void Encrypt(); void Decrypt();} public class Document : IStorableCompressible, IEncryptable{// bộ khởi tạo lớp Document lấy một tham sốpublic Document( string s){ Console.WriteLine(“Creating document with: {0}”, s);}// thực thi giao diện IStorablepublic void Read(){ Console.WriteLine(“Implementing the Read Method for IStorable”);}public void Write( object o){ Console.WriteLine(“Implementing the Write Method for IStorable”);}public int Status{ get { return status; } set { status = value; }}// thực thi ICompressiblepublic void Compress(){ Console.WriteLine(“Implementing Compress”);}public void Decompress(){ Console.WriteLine(“Implementing Decompress”);}// thực thi giao diệnILoggedCompressible public voidLogSavedBytes(){Console.WriteLine(“ImplementingLogSavedBytes”); } // 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 dữ 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.Lo ...

Tài liệu được xem nhiều: