Cơ chế Delegate & event
Số trang: 43
Loại file: ppt
Dung lượng: 1.23 MB
Lượt xem: 18
Lượt tải: 0
Xem trước 5 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
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.
Nội dung trích xuất từ tài liệu:
Cơ chế Delegate & eventCơ chế Delegate & EventNộ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 2Delegate 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 3Delegate 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 4Define delegate public delegate void MyDelegate1(int x, int y) Delegate cho dạng hàm: void Method( int, int ) public delegate string MyDelegate2(float f) Delegate cho dạng hàm: string Method( float ) 5Instance delegate public void Method1(int x, int y) { … } … MyDelegate1 del1 = new MyDelegate1(Method1); public string Method2(float f) { … } … MyDelegate2 del2 = new MyDelegate2(Method2); 6Call 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); 7Multi Castvoid 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);mulDelmulDel(5,10); 89Problem Xây dựng hàm Sort tổng quát cho mảng đối tượng có kiểu bất kỳ 10Solution 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 11Solution 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 12Solution Mô tả delegate CompareObj cho hàm Sort: Tên của delegate public delegate bool CompareObj(object o1,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 13Solution Delegate sẽ trỏ tới hàmĐịnh nghĩa hàm Sort tổng Compare riêng của lớpquát cho các 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] ); } } 14Solution 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); … Person[ ] persons = new Person[4]; persons[0] = new Person(“Quy Mui, 2, 2004); persons[1] = new Person(“Ha Lam, 65, 1978); persons[2] = new Person(“Ngoc Thao, 47, 1979); persons[3] = new Person(“Quoc Trung, 65, 1932); CompareObj cmp = new CompareObj(Person.CompareName); Lib.Sort( persons, cmp ); Gọi hàm Sort Lớp chứa hàm Sort ...
Nội dung trích xuất từ tài liệu:
Cơ chế Delegate & eventCơ chế Delegate & EventNộ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 2Delegate 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 3Delegate 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 4Define delegate public delegate void MyDelegate1(int x, int y) Delegate cho dạng hàm: void Method( int, int ) public delegate string MyDelegate2(float f) Delegate cho dạng hàm: string Method( float ) 5Instance delegate public void Method1(int x, int y) { … } … MyDelegate1 del1 = new MyDelegate1(Method1); public string Method2(float f) { … } … MyDelegate2 del2 = new MyDelegate2(Method2); 6Call 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); 7Multi Castvoid 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);mulDelmulDel(5,10); 89Problem Xây dựng hàm Sort tổng quát cho mảng đối tượng có kiểu bất kỳ 10Solution 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 11Solution 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 12Solution Mô tả delegate CompareObj cho hàm Sort: Tên của delegate public delegate bool CompareObj(object o1,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 13Solution Delegate sẽ trỏ tới hàmĐịnh nghĩa hàm Sort tổng Compare riêng của lớpquát cho các 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] ); } } 14Solution 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); … Person[ ] persons = new Person[4]; persons[0] = new Person(“Quy Mui, 2, 2004); persons[1] = new Person(“Ha Lam, 65, 1978); persons[2] = new Person(“Ngoc Thao, 47, 1979); persons[3] = new Person(“Quoc Trung, 65, 1932); CompareObj cmp = new CompareObj(Person.CompareName); Lib.Sort( persons, cmp ); Gọi hàm Sort Lớp chứa hàm Sort ...
Tìm kiếm theo từ khóa liên quan:
Cơ chế Delegate Cơ chế publishing kỹ thuật lập trình căn bản bài giảng môn lập trình ngôn ngữ lập trình C lập trình hướng đối tượngGợi ý tài liệu liên quan:
-
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 276 0 0 -
101 trang 200 1 0
-
Tìm hiểu về ngôn ngữ lập trình C: Phần 1 - Quách Tuấn Ngọc
211 trang 149 0 0 -
14 trang 134 0 0
-
161 trang 130 1 0
-
Giáo trình Vi điều khiển PIC: Phần 1
119 trang 116 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 -
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 -
Đồ án vi xử lý đề tài : nghiên cứu thiết kế mạch đo khoảng cách sử dụng vi điều khiển Pic 16F887
45 trang 97 1 0 -
Giáo trình Lập trình Windows 1 - Trường CĐN Đà Lạt
117 trang 96 0 0