Preface:
This essay is called an essay instead of an article. That is to say, this article is just my personal superficial understanding of technology. improper or wrong things are inevitable. I am very grateful to you for your advice.
Concept
1. As defined in MSDN:
The interface describes a set of related functions that can belong to any aspx "> class or structure.
What is a group of related functions is actually a common action. So why do we need to abstract this common behavior into an interface?
This will start from the following:
1. Specifications and constraints
There is an old saying in China that "no rules cannot be square". After thousands of years of inheritance, it proves that this sentence still makes sense. The same train, the same book and text, and the same question. Only when the economy can develop and society can progress. This fully demonstrates the importance of norms and constraints. Examples in real life are also everywhere, such as power outlets in the home,There are only two kinds of interfaces. Imagine what it would look like if different electrical products have their own power outlet standards and do not comply with such constraints? For example, we often use data lines for digital products. I always want to use a single line for all products!After talking about a bunch of things, I just want to talk about norms and constraints, which is advantageous under specific circumstances.This is one of the reasons why we need to abstract this common behavior to an interface.
2. Implementation of Polymorphism
For more information about polymorphism, see MSDN. If you can understand it, you can understand how much it is, and how much it can be done by yourself. You can implement this polymorphism through interfaces.
View interfaces in C # Language
I don't want to talk much about how to use interfaces. There are many articles in the garden for your reference. Here I want to talk about my understanding of the interface through the explicit interface and access modifier layer.
We all know that the Members defined in the interface do not have access modifiers. For example, if you want to add an access modifier before void doWork (), the compilation will not pass.
1: interface IControl
2: {
3: void doWork();
4: }
But why is it designed like this? At this time, someone said, isn't that nonsense! There is no need to use an access modifier for an interface. The interface is implemented by people. It makes no sense if the interface members are set to modifiers other than public. Therefore, when designing C #, Microsoft designs the interface as an access modifier that does not need to be used. However, the underlying layer is all public, which can be seen in the IL code, for example:
Therefore, when a class implements this interface, the doWork () method must also be public. If not, it cannot be compiled.
1: class Worker : IControl