1. Trang chủ
  2. » Công Nghệ Thông Tin

Giáo trình hướng dẫn truy cập phương thức gán đối tượng cho một giao diện đối lập trừu tượng p3

5 1 0

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

THÔNG TIN TÀI LIỆU

Nội dung

Những người phát triển sau có thể dẫn xuất một kiểu dữ liệu mới từ lớp Document, có thể là lớp Note hay lớp EmailMessage, và những người này mong muốn lớp Note đọc và viết vào cơ sở dữ liệu hơn là vào một tập tin. Ví dụ 8.4 mở rộng từ ví dụ 8.3 và minh họa việc phủ quyết một thực thi giao diện. Phương thức Read() được đánh

Ngơn Ngữ Lập Trình C# // 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 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”); 183 Thực Thi Giao Diện Ngôn Ngữ Lập Trình C# } // 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(); 184 Thực Thi Giao Diện Ngôn Ngữ Lập Trình C# } 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(); isc.LogSavedBytes(); // IStorableCompressible // 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”); } } 185 Thực Thi Giao Diện Ngôn Ngữ Lập Trình C# }  Kết quả: 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ụ 8.2 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 ILoggedCompressible 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, khơng thể 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 186 Thực Thi Giao Diện Ngơn Ngữ Lập Trình C# isc.LogSaveBytes(); // ILoggedCompressible isc.Compress(); // ICompress isc.Read(); // IStorable } Truy cập phương thức giao diện Chúng ta truy cập thành viên giao diện IStorable thể thành viên lớp Document: Document doc = new Document(“Test Document”); doc.status = -1; doc.Read(); ta tạo thể giao diện cách gán đối tượng Document cho kiểu liệu giao diện, sau sử dụng giao diện để truy cập phương thức: IStorable isDoc = (IStorable) doc; isDoc.status = 0; isDoc.Read(); Ghi chú: Cũng nói trước đây, khơng thể tạo thể giao diện cách trực tiếp.Do thực sau: IStorable isDoc = new IStorable(); Tuy nhiên tạo thể lớp thực thi sau: Document doc = new Document(“Test Document”); Sau tạo thể giao diện cách gán đối tượng thực thi đến kiểu liệu giao diện, trường hợp IStorable: IStorable isDoc = (IStorable) doc; Chúng ta kết hợp bước sau: IStorable isDoc = (IStorable) new Document(“Test Document”); Nói chung, cách thiết kế tốt định truy cập phương thức giao diện thông qua tham chiếu giao diện Do cách tốt sử dụng isDoc.Read(), sử dụng doc.Read() ví dụ trước Truy cập thông qua giao diện cho phép đối xử giao diện cách đa hình Nói cách khác, tạo hai hay nhiều lớp thực thi giao diện, sau cách truy cập lớp thông qua giao diện Gán đối tượng cho giao diện Trong nhiều trường hợp, khơng biết trước đối tượng có hỗ trợ giao diện đưa Ví dụ, giả sử có tập hợp đối tượng Document, vài đối tượng lưu trữ số lại chưa Và giả sử thêm giao diện giao diện thứ hai, ICompressible cho đối tượng để nén liệu truyền qua mail nhanh chóng: interface ICompressible { 187 Thực Thi Giao Diện ... thực thi giao diện, sau cách truy cập lớp thông qua giao diện Gán đối tượng cho giao diện Trong nhiều trường hợp, trước đối tượng có hỗ trợ giao diện đưa Ví dụ, giả sử có tập hợp đối tượng Document,... 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 ILoggedCompressible... tốt định truy cập phương thức giao diện thông qua tham chiếu giao diện Do cách tốt sử dụng isDoc.Read(), sử dụng doc.Read() ví dụ trước Truy cập thơng qua giao diện cho phép đối xử giao diện cách

Ngày đăng: 08/05/2021, 13:53

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN