Danh mục

Giáo trình hình thành ứng dụng kỹ năng gán đối tượng cho một giao diện đối lập p2

Số trang: 10      Loại file: pdf      Dung lượng: 1.35 MB      Lượt xem: 8      Lượt tải: 0    
Jamona

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu giáo trình hình thành ứng dụng kỹ năng gán đối tượng cho một giao diện đối lập p2, khoa học xã hội, thư viện thông tin phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Giáo trình hình thành ứng dụng kỹ năng gán đối tượng cho một giao diện đối lập p2 . 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ằn ...

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