Cơ chế Delegate & event

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. | Cơ chế Delegate & Event fdsfsd dsfsd das 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 fdsfsd dsfsd das 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 fdsfsd dsfsd das Delegate xem hàm như một đối tượng và truyền tham chiếu phương thức như là tham số bình thương, khong sử Dụng kiểu con trỏ hàm như C/C++ 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 Define delegate public delegate void MyDelegate1(int x, int y) Delegate | Cơ chế Delegate & Event fdsfsd dsfsd das 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 fdsfsd dsfsd das 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 fdsfsd dsfsd das Delegate xem hàm như một đối tượng và truyền tham chiếu phương thức như là tham số bình thương, khong sử Dụng kiểu con trỏ hàm như C/C++ 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 Define 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 ) Instance delegate public void Method1(int x, int y) { } MyDelegate1 del1 = new MyDelegate1(Method1); public string Method2(float f) { } MyDelegate2 del2 = new MyDelegate2(Method2); Call Delegate int x = 5, y = 10; del1(x, y); del1(10, 20); int y = 2; del1(100, y); Gọi del1 float f =; string s; s = del2(f); string s = del2(100f); Gọi del2 Multi Cast void Print(int x,int y) { (“x = {0}, y = {1}”, x, y); } void Sum(int x, int y) { (“Tong = {0}”, x+y); } MyDelegate1 mulDel = new MyDelegate1(Print); mulDel += new MyDelegate1(Sum); mulDel(5, 10); mulDel -= new MyDelegate1(Print); mulDel(5,10); Demo 1 Problem Xây dựng hàm Sort tổng quát cho mảng đối tượng có kiểu bất kỳ fdsfsd dsfsd das Vấn đề của bài toán này: Giải pháp dùng delegate cho mỗi lớp Solution Phân tích Nếu đối tượng là kiểu số như int, long, float thì ko

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
4    367    1    27-04-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.