TAILIEUCHUNG - Anonymous Methods and Delegates

Chưa xác định phương pháp và đại biểu Tất cả các ví dụ về cách thêm một phương pháp để một đại biểu mà bạn đã thấy cho đến nay sử dụng tên của phương pháp. Ví dụ, trở về kịch bản nhà máy tự động hiển thị trước đó | Anonymous Methods and Delegates All the examples of adding a method to a delegate that you have seen so far use the method s name. For example returning to the automated factory scenario shown earlier to add the StopFolding method of the folder object to the stopMachinery delegate we did this This approach is very useful if there is a convenient method that matches the signature of the delegate but what if this is not the case Suppose that the StopFolding method actually had the following signature void StopFolding int shutDownTime Shut down within the specified number of seconds This is now different from the FinishWelding and PaintOff methods therefore we cannot use the same delegate to handle all three methods. Creating a Method Adapter The way around this problem is to create another method that calls StopFolding but that takes no parameters itself like this void FinishFolding O Shutdown immediately __ NOTE The FinishFolding method is a classic example of an Adapter a method that converts or adapts a method to give it a different signature. This pattern is very common and is one of the set of patterns documented in the book Design Patterns Elements of Reusable Object-Oriented Architecture by Gamma Helm Johnson and Vlissides Addison-Wesley Professional 1994 . In many cases adapter methods such as this are small and it is easy to lose them in a sea of methods especially in a large class. Furthermore apart from using it to adapt the StopFolding method for use by the delegate it is unlikely to be called elsewhere. C provides anonymous methods for situations such as this. Using an Anonymous Method as an Adapter An anonymous method is a method that does not have a name. This sounds very strange but anonymous methods are actually quite useful. There might well be occassions when you have a block of code that you are never going to call directly but you would like to be able to invoke using a delegate. The .

TÀI LIỆU MỚI ĐĂNG
31    240    0    23-04-2024
19    228    0    23-04-2024
46    187    0    23-04-2024
14    170    0    23-04-2024
10    155    0    23-04-2024
15    183    0    23-04-2024
37    154    0    23-04-2024
10    116    0    23-04-2024
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.