The function of the < >asp.net interface (example explained)

Source: Internet
Author: User

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" to achieve what function, inheriting its classes can add their own methods.
2. Using an interface allows you to inherit its class: Named Unity/Specification, easy to maintain. For example: Two classes of "dog" and "cat" if it
Have inherited 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
That is, the name compilation does not pass by the interface constraint.

3. Provide a permanent interface. When a class is added, the existing interface methods can satisfy most of the methods in the inheriting class, and there is no need
It also saves code and improves development efficiency by re-designing a set of methods for new classes.
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
The functionality 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 inheriting it can call this method, which can call different classes depending on the class
Methods in the. 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 you inherit a lot of its classes, how many benefits are imaginable!

In addition, there are situations where an abstract class can be implemented but the interface is not implemented:

1. You can add code logic in an abstract class, but the interface cannot.

2. If you want to add a method to the interface, all classes that implement it are forced to reload this method again, and if you overload the Class A lot, it will increase the workload.

In short, generally in the only implementation of single-inheritance purposes, as far as possible with the abstract class, the function is more powerful!

The function of the < >asp.net interface (example explained)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.