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
Số trang: 5
Loại file: pdf
Dung lượng: 444.27 KB
Lượt xem: 6
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
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
Nội dung trích xuất từ tài liệu:
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 Ngôn Ngữ Lập Trình C# // 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 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 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(); 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(); // không thể gọi được } 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”); } } 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 bằng việc thực thi giao diện IStorable và giao diện ICompressible. Sau đó là phần mở rộng đến giao diện ILoggedCompressible rồi sau đó kết hợp cả hai vào giao diện IStorableCompressible. Và giao diện cuối cùng trong ví dụ là IEncrypt. Chương trình Tester tạo đối tượng Document mới và sau đó gán lần lượt vào các giao diện khác nhau. Khi một đối tượng được gán cho giao diện ILoggedCompressible, chúng ta có thể dùng giao diện này để gọi các phương thức của giao diện ICompressible bởi vì ILogged- Compressible mở rộng và thừa kế các phương thức từ giao diện cơ sở: ILoggedCompressible ilcDoc = doc as ILoggedCompressible; if ( ilcDoc != null ) { ilcDoc.LogSavedBytes(); ilcDoc.Compress(); // ilcDoc.Read(); // không thể gọi được } Tuy nhiên, ở đây chúng ta khôn ...
Nội dung trích xuất từ tài liệu:
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 Ngôn Ngữ Lập Trình C# // 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 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 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(); 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(); // không thể gọi được } 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”); } } 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 bằng việc thực thi giao diện IStorable và giao diện ICompressible. Sau đó là phần mở rộng đến giao diện ILoggedCompressible rồi sau đó kết hợp cả hai vào giao diện IStorableCompressible. Và giao diện cuối cùng trong ví dụ là IEncrypt. Chương trình Tester tạo đối tượng Document mới và sau đó gán lần lượt vào các giao diện khác nhau. Khi một đối tượng được gán cho giao diện ILoggedCompressible, chúng ta có thể dùng giao diện này để gọi các phương thức của giao diện ICompressible bởi vì ILogged- Compressible mở rộng và thừa kế các phương thức từ giao diện cơ sở: ILoggedCompressible ilcDoc = doc as ILoggedCompressible; if ( ilcDoc != null ) { ilcDoc.LogSavedBytes(); ilcDoc.Compress(); // ilcDoc.Read(); // không thể gọi được } Tuy nhiên, ở đây chúng ta khôn ...
Tìm kiếm theo từ khóa liên quan:
giáo trình đại học tài liệu mạng giáo trình cơ điện giáo trình thiết kế tài liệu kế toánGợi ý tài liệu liên quan:
-
Giáo trình phân tích một số loại nghiệp vụ mới trong kinh doanh ngân hàng quản lý ngân quỹ p5
7 trang 470 0 0 -
MARKETING VÀ QUÁ TRÌNH KIỂM TRA THỰC HIỆN MARKETING
6 trang 296 0 0 -
122 trang 212 0 0
-
QUY CHẾ THU THẬP, CẬP NHẬT SỬ DỤNG CƠ SỞ DỮ LIỆU DANH MỤC HÀNG HÓA BIỂU THUẾ
15 trang 200 1 0 -
BÀI GIẢNG KINH TẾ CHÍNH TRỊ MÁC - LÊNIN - TS. NGUYỄN VĂN LỊCH - 5
23 trang 198 0 0 -
Giáo trình chứng khoán cổ phiếu và thị trường (Hà Hưng Quốc Ph. D.) - 4
41 trang 192 0 0 -
Giáo trình hướng dẫn phân tích các thao tác cơ bản trong computer management p6
5 trang 188 0 0 -
BÀI GIẢNG LÝ THUYẾT MẠCH THS. NGUYỄN QUỐC DINH - 1
30 trang 169 0 0 -
Giáo trình phân tích giai đoạn tăng lãi suất và giá trị của tiền tệ theo thời gian tích lũy p10
5 trang 166 0 0 -
HỌC VIỆN CÔNG NGHỆ BƯU CHÍNH VIỄN THÔNG - NGÂN HÀNG ĐỀ THI HẾT HỌC PHẦN HỌC PHẦN: TOÁN KINH TẾ
9 trang 161 0 0