Interface call Methods
Basic knowledge
Interface Definition:
An interface is a set of named method signatures. Therefore, methods, attributes, and events can be defined in interfaces, because these are essentially methods. However, the interface cannot define Any constructor.
Interface accessibility:
Class modifier can be public or internal (tried in VS2008 ). However, when the interface method is displayed, the interface member cannot add any modifiers (tried in VS2008 ).
Interface inheritance and calling
Implicit implementation interface method:
Example:
Defines the Base class, which implements the IDisposable
// This class is derived from Object and it implements IDisposableinternal class Base: IDisposable {
// This method is implicitly sealed and cannot be overriddenpublic void Dispose () {Console. WriteLine ("Base's Dispose ");}
} // This class is derived from Base and it re-implements IDisposableinternal class Derived: Base, IDisposable {
// This method cannot override Base's Dispose. 'new' is used to indicate // that this method re-implements IDisposable's Dispose methodnew public void Dispose () {Console. writeLine ("Derived's Dispose"); // NOTE: The next line shows how to call a base class's implementation (if desired) // base. dispose ();}
}
Result call display:
Public static void Main ()
{
/************************ First Example ************ *************/
Base B = new Base (); // call Dispose with the B type: Result Base's Disposeb. dispose (); // call Dispose with the type of the object to which B points, // because the IDisposable Dispose method of the object type of B is implemented by the object of B //, the result is: Base's Dispose, which is the same as (IDisposable) B above ). dispose ();
/************************ Second Example ************ ************/
Derived d = new Derived (); // call Dispose with the d type, result; Derived's Disposed. dispose (); // call Dispose with the type of the object pointed to by d. The result is: "Derived's Dispose" (IDisposable) d ). dispose ();
/************************* Third Example ************ *************/
B = new Derived (); // call Dispose with the B type. The B type is Base. The result is: Base's Disposeb. dispose (); // call Dispose with the type of the object referred to by B. The object referred to by B is Derived, so the result is: Derived's Dispose (IDisposable) B ). dispose ();
}
In the method call of the preceding interface, pay attention to two concepts: when calling an interface method, whether the type indicated by the object is called or whether the type is called.
The above Base class and the Dispose method in the Derived class both implicitly implement the Dispose method in the interface IDisposable. If you implement the display of the Dispose method in the interface IDisposable, you can see this more clearly.
Now the code is displayed.
Display interface method implementation
Internal class Base: IDisposable
{
// This method is implicitly sealed and cannot be overridden
Public void Dispose ()
{
Console. WriteLine ("Base's Dispose ");
}
/// <Summary> // The Base display implements IDisposable // </summary> void IDisposable. Dispose (){
Console. WriteLine ("Base-> IDisposable. Dispose ");
}
}
Internal class Derived: Base, IDisposable
{
// This method cannot override Base's Dispose. 'new' is used to indicate
// That this method re-implements IDisposable's Dispose method
New public void Dispose ()
{
Console. WriteLine ("Derived's Dispose ");
// NOTE: The next line shows how to call a base class's implementation (if desired)
// Base. Dispose ();
}
/// <Summary> /// display the implementation of the Dispose method /// </summary> void IDisposable. Dispose (){
Console. WriteLine ("Derived-IDisposable.Dispose ");
}
}
If the same call code is as follows, the result is:
Public static void Main () {/************************* First Example *********** **************/
Base B = new Base (); // call Dispose with the B type. The B type is Base, and the public method of the call type is public void Dispose (): result Base's Disposeb. dispose (); // call Dispose with the type of the object referred to by B. // The Dispose method of IDisposable is implemented by B's object display, it is used with the public methods in the Base class.
// Public void Dispose () is irrelevant. It will call the void IDisposable. Dispose () method of the interface type IDisposable
// The result is: Base-> IDisposable. Dispose, which is different from the preceding one.
(IDisposable) B). Dispose ();
/************************ Second Example ************ ************/
Derived d = new Derived (); // call Dispose with the d type. The d type is Derived, which calls the public void Dispose () method (). The result is Derived's Disposed. Dispose (); // call Dispose with the type of the Object d. The d object type is displayed and converted to IDisposable, so it will call
// Void IDisposable. Dispose () method, so the result is "Derived-IDisposable.Dispose"
(IDisposable) d). Dispose ();
/************************* Third Example ************ *************/
B = new Derived (); // call Dispose with the B type. The B type is Base. The result is: Base's Disposeb. dispose (); // call Dispose with the type of the object referred to by B. The object referred to by B is Derived, and the object of the Derived type is forcibly converted to the interface type IDisposable. // It calls void IDisposable. Dispose (). So the result is: "Derived-IDisposable.Dispose" (IDisposable) B). Dispose ();
}
Publish via Wiz