Document directory
- 1. Public method implementation Interface Method
- 2. Private methods cannot implement interface methods
I recently saw an article:
Http://zhenyulu.cnblogs.com/archive/2006/04/18/377705.html
C # Interface
(Incomplete excerpt)
This article uses the following four cases to analyze how the interfaces in C # Work.
1. Public method implementation Interface Method
Although C # does not specify the access control method of the interface method when defining an interface, the default Interface methods are all public (this can be seen from the decompiled il code ). The following is the Il code of the interface viewed by reflector.
.class private interface abstract auto ansi IControl{ .method public hidebysig newslot abstract virtual instance void Paint() cil managed { }}
The class that implements the interface must implement all interface methods. Generally, the interface implementation method is also public. Example:
using System ;interface IControl { void Paint();}public class EditBox: IControl { public void Paint() { Console.WriteLine("Pain method is called!"); }}class Test { static void Main() { EditBox editbox = new EditBox(); editbox.Paint(); ((IControl)editbox).Paint(); }}
The execution result of the program is:
Pain method is called!Pain method is called!
An interface is like a one-to-multiple table in a relational database. An interface corresponds to multiple interface methods, and each interface method corresponds to a public or private method in a virtual method table (VMT. The image of the above Code in the memory can be described:
We can see the call to the paint method and the call to the paint method through the interface. It can be seen that calling a method through an interface requires an extra conversion, so the execution efficiency is not as good as calling it directly.
2. Private methods cannot implement interface methods
If you want to directly implement an interface method as a private method, you cannot do it. In the following editbox code, the paint method has no special instructions. The default value is private, leading to Code failure:
using System ;interface IControl { void Paint();}public class EditBox: IControl { void Paint() { Console.WriteLine("Pain method is called!"); } public void ShowPaint() { this.Paint(); ((IControl)this).Paint(); }}class Test { static void Main() { EditBox editbox = new EditBox(); editbox.ShowPaint(); }}
During compilation, the program displays the following compilation error: "editbox" does not implement "iControl. Paint ()" as the interface member ()". "Editbox. Paint ()" is static, non-public, or has an error return type ."
Why?
This is because the default access permission of the methods in the interface specification is public, and the default access permission in the class is default, that is, Private. Therefore, the permission range is reduced, and the permissions are not the same, therefore, you must change the class permission to public to execute the above Code.
Then the CPP vtable rule is applied ..
# Include <iostream>
Using namespace STD;
Class iControl
{
Public:
Virtual void paint () = 0;
};
Class editbox: Public iControl
{
PRIVATE:
Void paint ()
{
Cout <"SADS" <Endl;
}
Public:
Void showpaint ()
{
This-> paint ();
(IControl *) This)-> paint ();
}
};
Int main (INT argc, char * argv [])
{
Editbox * editbox = new editbox ();
Editbox-> showpaint ();
Return 0;
}
It is appropriate under CPP... as I expected ..
CPP vatble rewriting does not have access control.