C # basic knowledge sorting: Basic knowledge (9) Application of interfaces

Source: Internet
Author: User

Previously, I have been familiar with the concept of an interface. In fact, an interface is a set of methods and attributes. Whoever inherits it will implement this set of methods and attributes. That is to say, this class has some ability to define this interface.
This feature of interfaces plays a major role in programming. It often requires many people to complete a large project at the same time. In this way, some classes need some methods, the execution process is similar. This is why it is necessary to abstract this method into an interface during the project design stage. Then, in the project, you only need to implement this interface to prevent everyone from defining their own interfaces, the same operation is confusing.
For example, many windows are used for desktop applications, and events are registered in Windows (this is generally used for event transfer between different controls ), it also sticks to the skin (that is, taking the image as the background image of the controls on the form and form), and so on. This is the method required for every window. In this way, you can abstract an interface with a name:
Iwindowadditionalable.
For example, this interface is implemented when the useform window is written:

Interface:

Interface iwindowadditionalable {void pasteskin (); // paste void registerevent () to the control in the form; // register the event to be used}

Implementation interface code:

Public partial class useform: form, iwindowadditionalable // This interface is implemented for each form class written {public useform () {initializecomponent (); pasteskin (); registerevent ();} # region iwindowadditionalable member /// <summary> // method of skin pasting /// </Summary> Public void pasteskin () {This. btntest. backcolor = color. gray;} // <summary> // Method for event registration /// </Summary> Public void registerevent () {This. btntest. click + = new eventhandler (btntest_click);} void btntest_click (Object sender, eventargs e) {Throw new notimplementedexception ();} # endregion}

This interface is implemented in other Windows A, B..., so that it is more unified, and no method will be dropped. Of course, you can also write some other interfaces that meet certain requirements based on different project requirements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.