intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Lập trình windows C# - Chương 4

Chia sẻ: Tran Van Dinh | Ngày: | Loại File: PPT | Số trang:50

140
lượt xem
68
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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ả

Chủ đề:
Lưu

Nội dung Text: Lập trình windows C# - Chương 4

  1. Cơ chế Delegate & Event Nguyễn Văn Phong
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. Nguyễn Văn Phong - 2010 Nguy
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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)
  16. 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
  17. Nguyễn Văn Phong - 2010 Nguy
  18. Event Nguyễn Văn Phong - 2010 Nguy
  19. 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
  20. 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
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2