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 p3

Số trang: 10      Loại file: pdf      Dung lượng: 1.35 MB      Lượt xem: 11      Lượt tải: 0    
Thư viện của tui

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 p3, 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 p3. Ngôn Ngữ Lập Trình C# public void Read() {...} public void Write( object o) {...} //.... } Thực thi phủ quyết giao diện Khi thực thi một lớp chúng ta có thể tự do đánh dấu bất kỳ hay tất cả các phương thức thực thi giao diện như là một phương thức ảo. Ví dụ, lớp Document thực thi giao diện IStorable và có thể đánh dấu các phương thức Read() và Write() như là phương thức ảo. Lớp Document có thể đọc và viết nội dung của nó vào một kiểu dữ liệu File. 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 dấu như phương thức ảo và thực thi bởi Document.Read() và cuối cùng là được phủ quyết trong kiểu dữ liệu Note được dẫn xuất từ Document.  Ví dụ 8.4: Phủ quyết thực thi giao diện. ----------------------------------------------------------------------------- using System; interface IStorable { void Read(); void Write(); } // lớp Document đơn giản thực thi giao diện IStorable public class Document : IStorable { // bộ khởi dựng public Document( string s) { Console.WriteLine(“Creating document with: {0}”, s); } // đánh dấu phương thức Read ảo public virtual void Read() { Console.WriteLine(“Document Read Method for IStorable”); 193. Thực Thi Giao Diện. Ngôn Ngữ Lập Trình C# } // không phải phương thức ảo public void Write() { Console.WriteLine(“Document Write Method for IStorable”); } } // lớp dẫn xuất từ Document public class Note : Document { public Note( string s) : base(s) { Console.WriteLine(“Creating note with: {0}”, s); } // phủ quyết phương thức Read() public override void Read() { Console.WriteLine(“Overriding the Read Method for Note!”); } // thực thi một phương thức Write riêng của lớp public void Write() { Console.WriteLine(“Implementing the Write method for Note!”); } } public class Tester { static void Main() { // tạo một đối tượng Document Document theNote = new Note(“Test Note”); IStorable isNote = theNote as IStorable; if ( isNote != null) { isNote.Read(); isNote.Write(); } Console.WriteLine(“ ”); 194. Thực Thi Giao Diện. Ngôn Ngữ Lập Trình C# // trực tiếp gọi phương thức theNote.Read(); theNote.Write(); Console.WriteLine(“ ”); // tạo đối tượng Note Note note2 = new Note(“Second Test”); IStorable isNote2 = note2 as IStorable; if ( isNote != null ) { isNote2.Read(); isNote2.Write(); } Console.WriteLine(“ ”); // trực tiếp gọi phương thức note2.Read(); note2.Write(); } } -----------------------------------------------------------------------------  Kết quả: Creating document with: Test Note Creating note with: Test Note Overriding the Read method for Note! Document Write Method for IStorable Overriding the Read method for Note! Document Write Method for IStorable Creating document with: Second Test Creating note with: Second Test Overriding the Read method for Note! Document Write Method for IStorable Overriding the Read method for Note! Implementing the Write method for Note! ----------------------------------------------------------------------------- Trong ví dụ trên, lớp Document thực thi một giao diện đơn giản là IStorable: interface IStorable 195. Thực Thi Giao Diện. Ng ...

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