C#interface (Interface)
The interface defines the syntax contracts that should be followed when all classes inherit an interface. The interface defines the "what" part of the syntax contract, and the derived class defines the "How to" section of the syntax contract.
An interface defines properties, methods, and events, which are members of an interface. The interface contains only the declarations of the members. The definition of a member is the responsibility of the derived class. The interface provides the standard structure that a derived class should follow.
Abstract classes are somewhat similar to interfaces, but most of them are only used when only a few methods are declared by the base class by a derived class.
Interfaces are reference types, similar to classes, and are similar to abstract classes with three points:
1, can not be instantiated;
2, including the non-implementation of the method declaration;
3. Derived classes must implement methods that are not implemented, abstract classes are abstract methods, and interfaces are all members (not just methods including other members);
In addition, interfaces have the following characteristics: Interfaces can contain properties, indexers, events, and those members are defined as public, in addition to methods. In addition, you cannot include any other members, such as constants, fields, constructors, destructors, static members. A class can inherit directly from multiple interfaces, but only a single class (including abstract classes) is inherited directly.
the role of the C # interface
1. An interface is used to describe a common method/public property of a set of classes. It does not implement any method or property, just tells the class that inherits it, at least, what functions to implement, and the classes that inherit it can add their own methods.
2. Using an interface allows you to inherit its class: named Uniform/Specification, easy to maintain. For example: Two classes of "dog" and "cat", if they all inherit the interface "Animal", in which the animal has a method behavior (), then the dog and the cat must implement the behavior () method, And they are all named behavior so that there is no name too messy. If the name is not behavior (), the interface will constrain the naming of the compiler without being constrained by the interface constraint.
3. Provide a permanent interface. When the class is added, the existing interface method can satisfy most of the methods in the inheriting class, there is no need to re-design a set of methods for the new class, save the code and improve the development efficiency.
declaring interfaces
An interface uses the interface keyword declaration, which is similar to a class declaration. The interface declaration is public by default. The following is an instance of an interface declaration:
//public Interface: "Animals" Public InterfaceIAnimal {voidBehavior ();//behavioral methods that describe the characteristics of various animals} //class: Dog Public classdog:ianimal{ Public voidBehavior () {//Console.Write ("I Sleep at night, daytime activities");MessageBox.Show ("I sleep at night, daytime activities."); } } //class: Cat Public classcat:ianimal{ Public voidBehavior () {//Console.Write ("I sleep during the day, evening Activities");MessageBox.Show ("I sleep during the day, evening activities"); } }//easy to use: Public StaticMain () {Dog Mydog=NewDog (); Mydog.behavior (); //output: "I sleep at night, daytime activities"Cat Mycat =NewCat (); Mycat.behavior (); //output: "I sleep during the day, evening activities"}
The above call different classes of the same name method, will output a different east, that is, each class inside the same name method to complete the function can be completely different.
Further, it is not a method of invoking a class using the Main method above, which implements its invocation with polymorphism.
Take a look at the following method:
Public void Behavior (IAnimal myianimal) { myianimal.behavior ();}
Its argument is an interface type , and any class that inherits it can call this method, which can call methods in different classes depending on the class. can also be able to do their own according to different classes, the function of different classes.
Examples of polymorphism codes:
Newnew ////Behavior Accept "Dog" class instances
This way the behavior method writes one time to complete all the different functions of the same name method in the class that inherits it. Very convenient, again, due to "animal software" functional requirements, need to add a "turtle" class:
public Tortoise:ianimal { publicvoid Behavior () { MessageBox.Show (" I can sleep for 5,000 years without activity! ") " ); } }
It is also possible to invoke the above polymorphic method, so that the interface makes the method more extensible. If you inherit a lot of its classes, how many benefits are imaginable! (Source: http://blog.csdn.net/yunhaic/article/details/6662843)
Similarly, suppose our company has two kinds of programmer: VB programmer, refers to the programmer that writes program with VB, uses Clsvbprogramer this class to express, Delphi Programmer refers to the programmer that uses Delphi to write program, use Clsdelphiprogramer this class to express. Each class has a Writecode () method. Defined as follows:
Public classclsvbprogramer{ Public voidWritecode () {//write code in VB language; }} Public classclsdelphiprogramer{ Public voidWritecode () {//Write code in Delphi language; }} Public classclsproject{ Public voidWriteprograme (Clsvbprogramer programer)//write code in VB{programer. Writecode (); } Public voidWriteprograme (Clsdelphiprogramer programer)//overloaded methods, using Delphi to write code{programer. Writecode (); }}Static voidMain (string[] args) {Clsproject proj=NewClsproject (); //If you need to write code in VBClsvbprogramer Programer1 =NewClsvbprogramer (); Proj. Writeprograme (Programer1); //If you need to write code with DelphiClsdelphiprogramer Programer2 =NewClsdelphiprogramer (); Proj. Writeprograme (Programer2);}
But if the company came to a C # programmer, how do we change the program so that it can implement the function of writing programs in C #? We need to add a new class Clscsharpprogramer, and Clsproject this class again to reload the Writeprograme (Clscsharpprogramer programer) method. It's a lot of trouble. If there are C programmers, C + + programmers, Java programmers. It's too much trouble!
But if you switch to an interface, it's completely different:
First declare a programmer interface:
classProgram {Static voidMain (string[] args) {Clsproject proj=NewClsproject (); Iprogramer Programer; //If you need to write code in VBProgramer =NewClsvbprogramer (); Proj. Writeprograme (Programer); //If you need to write code with DelphiProgramer =NewClsdelphiprogramer (); Proj. Writeprograme (Programer); } } /// <summary> ///declaring Interfaces/// </summary> Public InterfaceIprogramer {voidWritecode (); } //then declare the class and implement the Iprogramer interface: /// <summary> ///Writecode method implemented by VB Programmer/// </summary> Public classClsvbprogramer:iprogramer { Public voidWritecode () {//write code in VB language;Console.WriteLine ("I am a VB programmer to achieve"); } } /// <summary> ///the Writecode method implemented by Delphi programmer/// </summary> Public classClsdelphiprogramer:iprogramer { Public voidWritecode () {//Write code in Delphi language;Console.WriteLine ("I was implemented by Delphi programmers."); } } /// <summary> ///Writecode method implemented by CSharp programmers/// </summary> Public classClscsharpprogramer:iprogramer { Public voidWritecode () {//Write code in Delphi language;Console.WriteLine ("I'm a csharp programmer."); } } /// <summary> ///Project/// </summary> Public classClsproject { Public voidwriteprograme (Iprogramer programer) {programer. Writecode ();//Write code } }
In this case, if any more programmers are added, we just need to add their related classes and then make a slight change in main (). Extensibility is particularly good!
In addition, if we clsproject this class into a component, then when our users need to expand the function, we only need to make a small external changes can be achieved, we can say there is no need to change the components we have sealed! This is very convenient, very powerful! (Source: http://blog.jobbole.com/85751/)
This article is purely personal learning, there are shortcomings please advise! (part of the article is from the network, if the infringement of your interests, please contact me, thank you.) )
Initial knowledge of C # interface