Giáo trình phân tích khả năng ứng dụng phương thức gán đối tượng cho một giao diện đối lập p5
Số trang: 5
Loại file: pdf
Dung lượng: 486.45 KB
Lượt xem: 7
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:
Tham khảo tài liệu giáo trình phân tích khả năng ứng dụng phương thức gán đối tượng cho một giao diện đối lập p5, kỹ thuật - công nghệ, điện - điện tử 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 phân tích khả năng ứng dụng phương thức gán đối tượng cho một giao diện đối lập p5. 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. ...
Nội dung trích xuất từ tài liệu:
Giáo trình phân tích khả năng ứng dụng phương thức gán đối tượng cho một giao diện đối lập p5. 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. ...
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ánTà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 471 0 0 -
MARKETING VÀ QUÁ TRÌNH KIỂM TRA THỰC HIỆN MARKETING
6 trang 300 0 0 -
122 trang 217 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 208 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 207 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 197 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 196 0 0 -
BÀI GIẢNG LÝ THUYẾT MẠCH THS. NGUYỄN QUỐC DINH - 1
30 trang 173 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 172 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 169 0 0