Essential C# 3.0 FOR NET FRAMEWORK 3.5 PHẦN 5

Một khi một lớp học tuyên bố rằng nó thực hiện một giao diện, tất cả các thành viên của giao diện phải được thực hiện. Việc thực hiện thành viên có thể ném một ngoại lệ NotImplementedException trong cơ thể phương pháp, nhưng dù sao, phương pháp này có thực hiện từ quan điểm của trình biên dịch. | Interface Implementation 303 result 0 else result if result 0 result return result endregion region IListable Members string get return new string FirstName LastName Phone Address endregion Once a class declares that it implements an interface all members of the interface must be implemented. The member implementation may throw a NotImplementedException type exception in the method body but nonetheless the method has an implementation from the compiler s perspective. One important characteristic of interfaces is that they can never be instantiated you cannot use new to create an interface and therefore interfaces cannot even have constructors or finalizers. Interface instances are available only from types that implement them. Furthermore interfaces cannot include static members. One key interface purpose is polymorphism and polymorphism without an instance of the implementing type is of little value. 304 Chapter 7 Interfaces Each interface member behaves like an abstract method forcing the derived class to implement the member. Therefore it is not possible to use the abstract modifier on interface members explicitly. However there are two variations on implementation explicit and implicit. Explicit Member Implementation Explicit members are accessible only when cast to the implemented interface. For example to call in Listing you must first cast the contact to IListable because of ColumnValues s explicit implementation. Listing Calling Explicit Interface Member Implementations string values Contact contactl contact2 . ERROR Unable to call ColumnValues directly on a contact. values First cast to IListable. values IListable contact2 .ColumnValues . The cast and the call to ColumnValues occur within the same statement in this case. Alternatively you could assign contact2 to an IListable variable before calling .

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
68    79    2    01-05-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.