tailieunhanh - Apress - Accelerated C#_2

Tham khảo tài liệu 'apress - accelerated c#_2', công nghệ thông tin, cơ sở dữ liệu phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | CHAPTER 5 INTERFACES AND CONTRACTS In this example note a couple of things. First FancyComboBox lists lUIControl in its inheritance list. That s how you indicate that FancyComboBox is planning to reimplement the lUIControl interface. Had lUIControl inherited from another interface FancyComboBox would have had to reimplement the methods from those inherited interfaces as well. I also had to use the new keyword for because it hides . This wouldn t have been a problem had ComboBox implemented the method explicitly because it wouldn t have been part of the ComboBox public contract. When the compiler matches class methods to interface methods it also considers public methods of base classes. In reality FancyComboBox could have indicated that it reimplements IUIControl but without redeclaring any methods as the compiler would have just wired up the interface to the base class methods. Of course doing so would be pointless because the reason you reimplement an interface in a derived class is to modify behavior. Note The ability to reimplement an interface is a powerful one. It highlights the vast differences between the way C and the CLR handle interfaces and the C treatment of interfaces as abstract class definitions. Gone are the intricacies of C vtables as well as the question of when you should use C virtual inheritance. As I ve said before and don t mind saying again C CLR interfaces are nothing more than contracts that say You Mr. Concrete Class agree to implement all of these methods in said contract . interface. When you implement methods in an interface contract implicitly they must be publicly accessible. As long as they meet those requirements they can also have other attributes including the virtual keyword. In fact implementing the IUIControl interface in ComboBox using virtual methods as opposed to nonvirtual methods would make the previous problem a lot easier to solve as demonstrated in the following .

TỪ KHÓA LIÊN QUAN