Inheriting "base class" and inheriting "interface" can implement some same functions, but some functions that can be accomplished by some interfaces are only used by the base class.
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 methods can satisfy most of the methods in the inheriting class, and there is no need to re-design a set of methods for the new class, save the code and improve the development efficiency.
To cite a code example:
Public interface: "Animals"
Public Interface IAnimal
{
int eyenumber;
private void Behavior (); Behavioral methods that describe the characteristics of various animals
}
Class: Dog
Public Dog:ianimal
{
String activetime = "Daytime";
private void Behavior ()
{
Console.Write ("I Sleep at night, daytime activities");
}
}
Class: Cat
Public Cat:ianimal
{
String activetime = "Night";
private void Behavior ()
{
Console.Write ("I sleep during the day, evening activities");
}
}
Easy to use:
public static Main ()
{
Dog Mydog = new Dog ();
Mydog.behavior (); Output: "I sleep at night, daytime activities"
Cat Mycat = new Cat ();
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 Behavior (IAnimal myianimal)
{
Myianimal.behavior ();
}
The parameter is the << interface type >> Any class that inherits it can call this method, which can invoke methods from 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:
Dog Mydog = new Dog ();
Cat Mycat = new Cat ();
Behavior (Mydog); Behavior accept "Dog" class instances
Behavior (MYCAT); 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,
Similarly, due to the "animal software" functional requirements, you need to add a "turtle" class:
Class: Turtle
Public Tortoise:ianimal
{
String activetime = "hard to say";
private void Behavior ()
{
Console.Write ("I can go to sleep for 5,000 years without moving!");
}
}
It is also possible to invoke the above polymorphic method, so that the interface makes the method more extensible. If the class that inherits it is many, how many benefits can be imagined!
C # Talking about the role of Interface (Interface)