An Invincible explanation of the interface!

Source: Internet
Author: User
Interface
Simply put, an interface is a contract or specification. for example, the State has introduced a national remote control specification, which is required in plain text to be followed by all remote control manufacturers. If the specification is not followed, the 3C Certification Mark will not be given, and the product cannot be sold on the market .. why is this specification required? We often encounter problems in time and life. The remote control of factory a cannot remotely control the TV of factory B, and the remote control of TV cannot remotely control other electrical appliances, such as air conditioners and refrigerators .! Why? Each remote control does not follow a standard. The radio waves have long and short voltage, and the voltage is high and low. As a result, the radio frequency is divided into four or five columns!
It can be imagined that the remote control standard for overseas users is only some important technical indicators for the remote control, such as how long the wave should be sent and how high the voltage should be ,..., but it will never regulate the material, shape, weight, and color of the remote control. It also means that the standard will discard all things irrelevant to the remote control! Each remote control manufacturer can interpret the remote control as long as it complies with the specifications. for example, Factory A can be made of iron, which is very powerful. Factory B can use paper and fold it at will. anyway, no matter what it is, what it looks like, as long as you follow the standard remote control, you can remotely control all the electrical appliances (of course, Electrical Manufacturers must follow certain specifications), and even remote control missile launch! This is the power of the interface.
For details, the interface is a specification, which has nothing to do with the specific implementation! An interface is a standard (virtual). It is just a piece of paper. In actual use, an interface makes sense only when it relies on an instance of a class that implements it, the remote control products made by various manufacturers above. each class (manufacturer) that implements an interface must implement all functions of the interface. once a class implements an interface, it can be said that a class is bundled with the interface (this is very important and will be used for the purpose of the question)
Here is an example.
Interface Remote Control specification // a nationally defined remote control specification, which must be implemented by each remote control manufacturer.
{
Int wavelength ();
Int voltage ();
}
Class A factory iron Remote Control: Remote Control specification // remote control implementation of Factory A (Interpretation) This specification is bound with the remote control specification! Okay, it can be sold in the market.
{
Public int wavelength (); // Standardized Indicator
Public int voltage (); // indicator defined in specifications
Public int shape () {square}; // Factory A's own interpretation of this product
Public int material () (iron}; // Factory A's own interpretation of this product
}
Class B Factory paper Remote Control: Remote Control specification // remote control implementation of Factory A (Interpretation) This specification is bound with the remote control specification! Okay, it can be sold in the market.
{
Public int wavelength (); // normalized indicator
Public int voltage (); // indicator defined in specifications
Public int shape () (Circular); // Factory A's own interpretation of this product, which is circular
Public int material () (paper); // Factory A's own interpretation of the product, made of paper, cool!
}
Class appliances
{Procedure receives Remote Control (remote control specification) // receives remote control commands on an electric appliance
{.....
Receiving (remote control specification. wavelength );
Receiving (remote control specification. Voltage );
.....}}
Static main ()
{
Factory A iron Remote Control controla; // declare the Controller object
Factory B paper Remote Control controlb;
Controla = new factory iron Remote Control (); // instantiate the Controller object. At this time, the system allocates space for the object in the managed stack.
Controlb = new factory B paper Remote Control ();
Remote control specification controlinterfacea = (remote control specification) Remote Control 1; // converts an object instance to a specification. Why? Because "My Home Appliances" can only identify remote control specifications, it cannot identify specific remote control
Remote control specification controlinterfaceb = (remote control specification) Remote Control 2; // same as above
Electric Appliances in my house = new electrical appliances ();
My electric appliance. receive Remote Control (controlinterfacea) // I use factory a remote control to remotely control my appliances. note: The controlinterfacea here cannot exist independently. It must depend on the instance "controla" that implements the "remote control specification" class ". the principle is very simple. The interface is a pointer and will not be allocated space, so you will not be able to use it. You only have to contact a specific instance of the class to have the available space.
My home appliances. receive Remote Control (controlinterfaceb) // I use factory B remote control to remotely control my Home Appliances

...
// Below is my imagination. I can use the remote control to control missile launch!
My missile. receive Remote Control (controlinterfacea );
My missiles. receive Remote Control (controlinterfaceb );
...
}
--------------------------------------------------------------------
Interface execution
Now that we have the interface concept, let's talk about how the C # program uses the interface during running and how to access the interface function. The specific process is as follows:
A. When a function of an interface is called, the system will check what the corresponding instance of this interface is?
B. Find this instance, and then find out what the instance class is (what is an instance class, refer to Reading Notes 2)
C. Check whether the instance class is bound with the interface based on the instance class (check whether the interface is implemented, followed by a colon)
D. Good! If the instance class implements this interface (a bundle occurs), it will find the function definition in this instance class, and then execute this function. The execution ends.
E. If not found, he will continue to find the parent class until the first parent class bound to the interface is found.
F. After finding the function, it checks whether the function is a virtual function,
G. If not, he will execute it immediately.
H. If yes, the system will start from scratch and check whether the function of the Instance class has been overloaded. For details about the process, see (C # Reading Notes 2 ).
Example:

Interface I
{
Void func ();
}

Class A: I
{
Public Virtual void func () {console. writeline ("funca ")};
}

Class B: A, I // What do you mean here?
{
Public void func () {console. writeline ("funcb ")};
}

Class C:
{
Public override void func () {console. writeline ("funcc ")};
}
Static main ()
{I A = new A (); // declares interface A and immediately associates with a class instance.
I B = new B (); // declares interface B and has a relationship with a class instance immediately.
I c = new C (); // declares the interface C, and immediately has a relationship with a class instance
A. func (); // check instance A of A and find that A is bound to interface I. Therefore, run the func function of A. The result is: funca.
B. func (); // check instance B of B and find that B is bound to interface I. Therefore, execute function func of B. Result: funcb
C. func (); // instance c of common C, found that it is not bound with interface I, the system continues to find its parent class. when I found that a and I were bound, he went to function a and found that a was a virtual function. The system looked for the class instance C from the beginning and found that C was overloaded with func, execute this function immediately. the result is funcc;
}

 

From:
Http://bbs.mvpcn.net/ShowPost.asp? Id = 111

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.