1. What is an interface
Interfaces can be seen as a standard, and all inherited subclasses need to follow the methods declared in the interface to
interface with the keyword interface decoration, the name of the interface is generally i.........able, indicating what I have the ability
The interface is usually under the namespace, that is, with the class is at the same level (the interface can also be placed in the class, but there is no meaning, because the interface can be regarded as a special class, the class is the solution, the interface is also, so put in the inside no cock meaning)
The method inside the interface, the property cannot be decorated with public,private etc.
The method inside the interface is not implemented, only declares that the true implementation is implemented in the subclass of the inheritance
Interface can only put properties, methods, can not put the field
Of course, these can all be observed in the code.
2. Next use the code to achieve polymorphism, and then observe, what characteristics
//first, declare an interface below the namespaceInterfaceianimalable {//There can be no field in the interface, there are methods, there are natural properties voidsay (); stringName {Get; Set; } }Interfaceipersonable {voidHisay (); }//an interface can inherit multiple interfaces at the same timeInterfaceistudentable:ianimalable,ipersonable {voidHisay (); }//Define a Parrot class under the namespace, inherit and interface classparrot:ianimalable {Private string_name; Public stringName {Get { return_name; } Set{_name=value; } }//The following is the invocation method of an explicit interface, which writes the method body in the following format//returns the value of the interface name. Function name () {method body}; voidIanimalable.say () {Console.WriteLine ("I am a parrot, my name is {0}", name); } }//define a dog class under the namespace, inherit with interface classdog:ianimalable {Private string_name; Public stringName {Get { return_name; } Set{_name=value; } } Public voidsay () {Console.WriteLine ("I am a dog, my name is {0}", _name); }
Define an interface variable, such as ianimalable animal = new Parrot (); Animal = new Dog ();
As you can see here, the interface is like a special class, the interface is not instantiated, but can be instantiated indirectly through subclasses
2.1 When the name of the method in the subclass of the inherited interface is the same as the method name in the interface, this requires the display interface, which can be seen in the code for specific usage
2.2 An interface can be inherited by multiple interfaces or multiple classes, an interface can inherit multiple interface inheritance at the same time, but a class cannot inherit multiple classes at the same time, an interface can inherit classes and interfaces at the same time, but the interface must be in the back
2.3 Animal first loaded is the Parrot class, animal later loaded is the dog class, installed that class, it shows the characteristics of that class
2.4 This interface shows a different form, can be a dog class, can be a parrot and so on, the realization of polymorphic
An overview of the implementation of CSharp polymorphism
Implementation of CSharp polymorphism (interface)