Home  >  Article  >  Backend Development  >  C# basic knowledge compilation: basic knowledge (9) application of interfaces

C# basic knowledge compilation: basic knowledge (9) application of interfaces

黄舟
黄舟Original
2017-02-11 13:19:521274browse

I have come across the concept of interface before. In fact, an interface is a set of methods and attributes. Whoever inherits it must implement this set of methods and attributes. In other words, this class has some capabilities defined by this interface.
This feature of the interface plays a great role in ordinary programming. Often, many people are required to complete a large project at the same time. In this way, it is inevitable that some classes will require certain methods, and the execution processes will be similar. This is why it is necessary to abstract this method into an interface during the project design stage. Then in the project, everyone only needs to implement this interface, thereby preventing everyone from defining their own, resulting in confusion in the same operation.
For example, when making a desktop application, many windows will be used. Events will be registered in the windows (this is generally used for event transfer between different controls), and skins will be attached (that is, pictures will be taken as forms and on the form). background image of the control), etc. This is the method that every window needs to use. Like this, you can abstract an interface and name it:
IWindowAdditionalAble.
For example, when writing a UseForm window, implement this interface:

Interface:

    interface IWindowAdditionalAble
    {
        void PasteSkin();//给窗体中的控件贴皮肤

        void RegisterEvent();//注册要用到的事件
    }

Implement the interface code:

 public partial class UseForm : Form, IWindowAdditionalAble//每写一个窗体类都实现该接口
    {
        public UseForm()
        {
            InitializeComponent();

            PasteSkin();

            RegisterEvent();
        }

        #region IWindowAdditionalAble 成员

        /// <summary>
        /// 贴皮肤的方法
        /// </summary>
        public void PasteSkin()
        {
            this.btnTest.BackColor = Color.Gray;
        }
        /// <summary>
        /// 注册事件的方法
        /// </summary>
        public void RegisterEvent()
        {
            this.btnTest.Click += new EventHandler(btnTest_Click);
        }

        void btnTest_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

In others Window A, B... all implement this interface, which is more unified and does not leave any method behind. Of course, you can also write some other interfaces that meet certain needs according to the needs of the project.

The above is the compilation of C# basic knowledge: Basic knowledge (9) Interface application. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn