We all know that virtual methods implement polymorphism, abstract methods to achieve polymorphism, and so on, we are today to see how to use the interface to achieve polymorphic
1. First we need to understand what is the interface, the consciousness of its existence
The interface is the one that exists to constrain the format of the method ( parameters and return value types ) .
the interface can implement multiple inheritance to compensate for the defects of single inheritance.
The interface can be regarded as a special abstract class, and the source code is seen by the anti-compilation .
the method in the interface does not have access modifiers because the CLR is automatically added and cannot have a method body
If a class implements an interface, it has to implement all the methods in that interface .
the interface should be used with caution to prevent contamination of the interface!
The interface represents only one capability, and the classes and interfaces that implement the interface do not have an inheritance relationship
The interface is for implementation, and the class is used for inheritance.
In fact, many times, seemingly can not interface, because the interface is a method of the Convention,
Indicates that you must have some method of this class, but do not write interfaces can also have these methods, using the interface,
You can use interface variables, unified invocation, to implement polymorphic
10. Only methods can be defined in an interface, and variables cannot be defined.
Let's give an example of using an interface to achieve polymorphism:
01. Create a new Fly interface
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceInterface {//Fly the interface Public InterfaceIFly {//The method in the interface does not have access modifiers because the CLR is automatically added and cannot have a method body voidSay (); }}
02. Create an Eating interface
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceInterface {//eating the interface Public InterfaceIEat {//The method in the interface does not have access modifiers because the CLR is automatically added and cannot have a method body voideat (); }}
03. Create a bird to implement the flying interface and the eating interface
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceInterface {//The Bird realizes the Flying interface and eats the interface Public classGrid:ifly,ieat//Note interfaces: Interfaces are called inheritance, classes: Interfaces are called implementations { //if a class implements an interface, it has to implement all the methods in that interface . Public voidSay () {Console.WriteLine ("The birds are flying ."); } Public voidEat () {Console.WriteLine ("the bird is eating"); } }}
04. Create an aircraft class to achieve the Flying interface
usingSystem;usingSystem.Collections.Generic;usingSystem.Globalization;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceInterface {//The plane realizes the flying interface Public classPlan:ifly { Public voidSay () {Console.WriteLine ("The plane is flying ."); } }}
05. In the Main method implementation call
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceInterface {classProgram {Static voidMain (string[] args) { //defining an array of fly interfaces instantiation objectsifly[] Iflies = { NewGrid (),NewPlan ()}; //Loop Array call method to implement polymorphism foreach(IFly IFlyinchiflies) {Ifly.say (); } //Bird Eating Instantiation ObjectIEat ieats =NewGrid (); //calling methods to implement polymorphismieats.eat (); Console.ReadLine (); } }}
This enables polymorphism and results in the following:
Note: If you have a class that implements two interfaces, it is unfortunate that the two interfaces (for example: Ifly,iplay) have the right two named methods (Eat)
We all know that.
If a class implements an interface, it has to implement all the methods in that interface.
What about this? The method of its own solution:
We can use the interface name. method to implement
// implements the iFLY and iplay two interfaces, but there are eat methods in all two interfaces Public class Dog:ifly,iplay { // If a class implements an interface, it has to implement all the methods in that interface // so that we can use the interface name. method to implement void ifly.eat () { } void iplay.eat () { } }
The interface in C # implements polymorphism