Lập trình windows C# - Chương 4
lượt xem 68
download
Tham khảo tài liệu 'lập trình windows c# - chương 4', 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ả
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Lập trình windows C# - Chương 4
- Cơ chế Delegate & Event Nguyễn Văn Phong
- Nội dung Delegate Khái niệm delegate Thực thi delegate Multicast delegate Giải pháp cho hàm Sort tổng quát Event Khái niệm event Event & delegate Cơ chế publishing & subscribing Minh họa cơ chế event Nguyễn Văn Phong - 2010 Nguy
- Delegate Lớp đóng gói các phương thức (method signature) Dùng trong event-handling model của C# Đặc tính Type safe Object oriented mechanism Delegate là class: Có instance Có thể chứa những tham chiếu đến 1 hay nhiều method Nguyễn Văn Phong - 2010 Nguy
- Delegate Một delegate định nghĩa một signature Return type Sequence of parameter types Tất cả các method có cùng signature có thể được add vào thể hiện của delegate Delegate instance có một danh sách các tham chiếu method Cho phép add (+) các method Có thể remove (-) các method Nguyễn Văn Phong - 2010 Nguy
- Define delegate public delegate void MyDelegate1(int x, int y) public MyDelegate1(int Delegate cho dạng hàm: Delegate void Method( int, int ) public delegate string MyDelegate2(float f) public MyDelegate2(float Delegate cho dạng hàm: Delegate string Method( float ) Nguyễn Văn Phong - 2010 Nguy
- Instance delegate public void Method1(int x, int y) { … } … MyDelegate1 del1 = new MyDelegate1(Method1); MyDelegate1 MyDelegate1 public string Method2(float f) { … } … MyDelegate2 del2 = new MyDelegate2(Method2); MyDelegate2 MyDelegate2 Nguyễn Văn Phong - 2010 Nguy
- Call Delegate Gọi del1 int x = 5, y = 10; int y = 2; del1(10, 20); del1(x, y); del1(100, y); Gọi del2 float f =0.5f; string s = del2(100f); string s; s = del2(f); Nguyễn Văn Phong - 2010 Nguy
- Multi Cast void Print(int x,int y) { Console.WriteLine(“x = {0}, y = {1}”, x, y); } void Sum(int x, int y) { Console.WriteLine(“Tong = {0}”, x+y); } MyDelegate1 mulDel = new MyDelegate1(Print); mulDel += new MyDelegate1(Sum); mulDel(5, 10); mulDel(5, mulDel -= new MyDelegate1(Print); mulDel mulDel(5,10); Nguyễn Văn Phong - 2010 Nguy
- Nguyễn Văn Phong - 2010 Nguy
- Problem Xây dựng hàm Sort tổng quát cho cho mảng đối tượng có kiểu bất kỳ Nguyễn Văn Phong - 2010 Nguy
- Solution Phân tích Nếu đối tượng là kiểu số như int, long, float thì ko có vấn đề Trường hợp đối tượng phức khác? So sánh theo So quy tắc nào quy Nguyễn Văn Phong - 2010 Nguy
- Solution Giải pháp: Cho phép đối tượng tự quy định thứ tự của chúng Sử dụng delegate để truyền phương thức so sánh này vào hàm Sort void Sort(object[] list, CompareObj cmp) Delegate này sẽ tham chiếu tới hàm Compare của lớp MyClass. Chính lớp MyClass sẽ quy định thứ tự của các đối tượng Nguyễn Văn Phong - 2010 Nguy
- Solution Mô tả delegate CompareObj cho hàm Sort: Tên của delegate public delegate bool CompareObj(object o1,object o2) public bool CompareObj object o2) Trả về true: nếu o1 “trước” o2 false: ngược lại 2 đối tượng cần so sánh Nguyễn Văn Phong - 2010 Nguy
- Solution Delegate sẽ trỏ tới Định nghĩa hàm Sort tổng hàm Compare riêng hàm quát cho các lớp của lớp tương ứng public static void Sort(object[] objs, CompareObj cmp) cmp { Yêu cầu lớp tự for(int i=0; i < objs.Length-1; i++) for(int i++) so sánh for(int j=objs.Length-1; j>i; j--) for(int j--) if ( cmp( objs[j], objs[j-1] ) ) if cmp objs[j], objs[j-1] { Swap( objs[j], objs[j-1] ); } } Nguyễn Văn Phong - 2010 Nguy
- Solution Các lớp hỗ trợ Sort thì phải Cung cấp hàm Compare riêng Signature phải thoả delegate CompareObj class Person { Cùng Cùng signature signature private string name; private private int weight; private private int yearOfBirth; private public static bool CompareName(object p1, object p2) { CompareName if (string.Compare(((Person)p1).name, ((Person)p2).name)
- Solution public delegate bool CompareObj(object o1,object o2); CompareObj … Person[ ] persons = new Person[4]; Person[ persons persons[0] = new Person(“Quy Mui", 2, 2004); persons[0] Mui 2, 2004); persons[1] = new Person(“Ha Giang", 65, 1978); persons[1] Giang 65, 1978); persons[2] = new Person(“Ngoc Thao", 47, 1979); persons[2] Thao 47, persons[3] = new Person(“Ha Nam", 65, 1932); persons[3] Nam 65, 1932); CompareObj cmp = new CompareObj(Person.CompareName); CompareObj cmp CompareObj MySort.Sort( persons, cmp ); MySort persons cmp ); Gọi hàm static Sort Lớp chứa hàm Sort Nguyễn Văn Phong - 2010 Nguy
- Nguyễn Văn Phong - 2010 Nguy
- Event Nguyễn Văn Phong - 2010 Nguy
- Event Cơ chế thông điệp giữa các lớp hay các đối tượng Có thể thông báo cho lớp khác biết được khi một lớp có phát sinh điều gì đó Publisher: lớp phát sinh sự kiện Subscriber: lớp nhận hay xử lý khi sự kiện xảy ra Nguyễn Văn Phong - 2010 Nguy
- Event Trong môi trường giao diện GUIs (Graphical User Interfaces: GUIs): Button đưa ra sự kiện “Click”, cho phép ”, lớp khác có thể đáp ứng (xử lý) khi sự kiện này xảy ra. VD: Button “Add” trong Form, khi sự kiện click xảy ra thì Form thực hiện lấy dữ liệu từ các TextBox đưa vào ListBox… Nguyễn Văn Phong - 2010 Nguy
CÓ THỂ BẠN MUỐN DOWNLOAD
-
Bài tập lập trình Windows dùng C# - Bài số 4
10 p | 560 | 110
-
C# and .NET
4 p | 235 | 102
-
Giáo trình Lập trình Window Phone (Module 1) - Trung tâm tin học ĐH KHTN
110 p | 250 | 80
-
Lập trinh Windows bằng Visual C++ part 4
45 p | 183 | 75
-
CÁC QUI ĐỊNH - Môn học: Lập trình C trên Windows
1 p | 298 | 57
-
Giáo trình Lập trình C trên Windows: Phần 2 - Nguyễn Đình Quyên, Mai Xuân Hùng (đồng biên soạn)
73 p | 156 | 50
-
Bài tập lập trình Windows dùng C# - đề thi thực hành
1 p | 365 | 45
-
Ví dụ và Bài tập Lập trình C trên Windows
20 p | 215 | 43
-
giới thiệu hợp ngữ Assembler very good phần 4
23 p | 113 | 41
-
Bài tập thực hành Lập trình trên môi trường Windows (Lập trình Windows Form với C#): Lab 4 - ĐH Công nghệ Tp.HCM
12 p | 290 | 40
-
Lập trình Windows Thư viện đồ họa GDI (phần 4)
7 p | 137 | 37
-
giáo trình Java By Example phần 4
35 p | 128 | 35
-
Windows Applications - Windows Forms – Phần 4
13 p | 95 | 25
-
giáo trình Acad phần 4
11 p | 86 | 22
-
kỹ thuật lập trình C chuyên nghiệp phần 4
24 p | 99 | 6
-
Giáo trình Lập trình window 1 (Nghề: Lập trình máy tính - Trình độ Cao đẳng) - Trường Cao đẳng Nghề An Giang
87 p | 25 | 5
-
Lập trình Java: Đa tuyến là gì ? phần 4
5 p | 71 | 4
-
Lecture Windows programming: Chapter 4 - Châu Thị Bảo Hà
72 p | 45 | 2
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn