Accelerated VB 2005 phần 8

Bạn trình bày một ví dụ DateTime, sau đó thực hiện DateTime () sẽ sử dụng một thể hiện của Lưu ý để xây dựng các ví dụ trước, bạn sẽ cần phải thêm một tham chiếu đến lắp ráp, nằm trong thư mục \ Framework \ . | 278 CHAPTER 13 GENERICS Return vall val2 End Function Private Shared Function DoubleToInt64 ByVal d As Double As Int64 Return d End Function End Class The previous example returns the following value for Magnitude Magnitude is 6 You may be looking at the previous code and wondering why the complexity seems much higher when all you re trying to do is find the magnitude of a complex number. As mentioned previously you have to provide a delegate to handle the multiplication external to the generic type. Thus you define the Complex Of T .Multiply delegate. At construction time the Complex Of T constructor must be passed a third parameter that references a method for the multiplication delegate to refer to. In this case handles the multiplication. So when the Magnitude property needs to multiply the components it must use the delegate rather than the multiplication operator. Naturally when the delegate is called it boils down to a call to the multiplication operator. However the application of the operator is now effectively external to the generic type Complex Of T . No doubt you noticed the extra complexities to the property accessor. First accepts a type of . This explains the call to the method. And to make sure things go smoothly you add a constraint to T so that the type supplied supports IConvertible. But you re not done yet. returns a and you have to convert that value type back into type T. In order to do so you cannot rely on the class because you don t know what type you re converting to at compile time. Yet again you have to externalize an operation which in this case is a conversion. This is one reason why the Framework defines the Converter Of TInput TOuput delegate. In this case Complex Of T needs a Converter Of Double T conversion delegate. At construction time you must pass a method for this delegate to call through to which in this .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
272    24    1    02-12-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.