Giáo trình hình thành hệ thống khởi tạo các giá trị ban đầu cho các biến thành viên p3
Số trang: 10
Loại file: pdf
Dung lượng: 1.35 MB
Lượt xem: 9
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 hình thành hệ thống khởi tạo các giá trị ban đầu cho các biến thành viên p3, công nghệ thông tin, kỹ thuật lập trình 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 hệ thống khởi tạo các giá trị ban đầu cho các biến thành viên 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. ...
Nội dung trích xuất từ tài liệu:
Giáo trình hình thành hệ thống khởi tạo các giá trị ban đầu cho các biến thành viên 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. ...
Tìm kiếm theo từ khóa liên quan:
giáo trình lập trình thủ thuật quản trị mạng kỹ năng lập trình phương pháp lập trình mẹo quản lập trìnhGợi ý tài liệu liên quan:
-
Giáo trình Lập trình logic trong prolog: Phần 1
114 trang 193 0 0 -
Giáo trình Lập trình C căn bản: Phần 1
64 trang 170 0 0 -
Giáo trình Lập trình C căn bản
135 trang 169 0 0 -
Thiết kế mạch logic bằng Verilog - HDL
45 trang 164 0 0 -
Hướng dẫn lập trình với Android part 4
5 trang 156 0 0 -
14 trang 134 0 0
-
142 trang 130 0 0
-
Bài giảng Phương pháp lập trình: Chương 9 - GV. Từ Thị Xuân Hiền
36 trang 112 0 0 -
Giáo trình lập trình hướng đối tượng - Lê Thị Mỹ Hạnh ĐH Đà Nẵng
165 trang 112 0 0 -
information technology outsourcing transactions process strategies and contracts 2nd ed phần 3
65 trang 111 0 0