Inheritance of "base class" and Inheritance of "interface" can implement some of the same functions, but some interfaces can complete the functions that cannot be implemented only by the base class.
1. The interface is used to describe the public methods/Public attributes of a group of classes. It does not implement any methods or attributes, but only tells the class that inherits it.
At least what functions should be implemented? The inherited classes can add their own methods.
2. You can use interfaces to make the classes that inherit from them: uniform names/specifications, which are easy to maintain. For example, if two classes are "dog" and "cat ",
All of them inherit the interface "animal", where the animal contains a method behavior (), then the dog and cat must implement the behavior () method,
And the names are named behavior, so that the names will not be too messy. If the names are not behavior (), the interface will constrain
That is, the compilation will not pass without naming according to interface constraints.
3. provides permanent interfaces. When a class is added, the existing interface methods can meet the requirements of most methods in the inherited class.
Re-design a set of methods for the new class, saving code and improving development efficiency.
Sample Code:
// Public interface: "animal"
Public interface ianimal
{
Int eyenumber;
Private Void Behavior (); // behavior method, which describes the characteristics of various animals
}
// Category: Dog
Public dog: ianimal
{
String activetime = "Daytime ";
Private Void Behavior ()
{{
Console. Write ("sleeping at night, daytime activity ");
}
}
// Category: Cat
Public Cat: ianimal
{
String activetime = "night ";
Private Void Behavior ()
{{
Console. Write ("I am sleeping during the day, evening activities ");
}
}
// Simple application:
Public static main ()
{
Dog mydog = new dog ();
Mydog. behavior (); // output: "I am sleeping at night, daytime activity"
Cat MyCat = new CAT ();
MyCat. behavior (); // output: "I am sleeping during the day, evening activities"
}
The above calls the Same Name method of different classes, will output different stuff, that is, the same name method in each class to complete
Functions can be completely different.
Furthermore, instead of calling class methods one by one using the preceding main method, the method is called using polymorphism.
Let's look at the following method:
Public behavior (ianimal myianimal)
{
Myianimal. behavior ();
}
Its parameter is <interface type>. Any class that inherits it can call this method. This method can call different classes according to different classes.
Method in. That is, you can complete the functions of different classes based on different classes.
Sample Code for Polymorphism:
Dog mydog = new dog ();
Cat MyCat = new CAT ();
Behavior (mydog); // behavior accepts "dog" instances
Behavior (MyCat); // behavior accepts "dog" instances
In this way, the behavior method can be written once to complete different functions of the same name method in all classes that inherit it. This is very convenient,
Similarly, due to the functional requirements of "animal software", another "Turtle" category needs to be added:
// Category: Turtle
Public tortoise: ianimal
{
String activetime = "Hard to say ";
Private Void Behavior ()
{{
Console. Write ("I can stay up for five thousand years after a sleep! ");
}
}
You can also call the above multi-state method, so the interface makes the method more scalable.
If there are many classes that inherit from it, what are the benefits!
In addition, there are situations where abstract classes can be implemented but interfaces cannot be implemented:
1. Code logic can be added to the abstract class, but the interface cannot.
2. If you want to add a method to the interface, all the classes that implement it will force the method to be reloaded. If there are many classes to be reloaded, the workload will increase.
In a word, abstract classes are used as much as possible when only single inheritance purposes are implemented!