Previous Article: www.2cto.com/kf/201111/410744.html
1. What is the interface definition?
A: An interface indicates a group of function members without implementing the reference type of members. Classes and structures can implement interfaces.
2. What is the meaning of the interface and why it should be used?
To understand the meaning of the interface and why it is useful, let's take a look at the following code, which accepts an unordered integer array and sorts it in ascending order.
Class Program
{
Static voidMain (string [] args)
{
Var myInt = new [] {20, 4, 16, 9, 2}; // create an integer array implicitly.
Array. Sort (myInt); // calls the Sort method of the Array class to Sort the Array. The default value is ascending.
Foreach (var item in myInt) // use the foreach statement to traverse the array.
{
Console. Write ("{0}", item );
Console. Write ("");
}
Console. ReadKey ();
}
}
The program output result is:
The Sort method works well on the int array, but what happens if we try to use it on our own class? The following code is used:
Class Program
{
Static voidMain (string [] args)
{
Var myInt = new [] {20, 4, 16, 9, 2}; // create an integer array implicitly.
MyClass [] mc = new MyClass [5]; // create an array of MyClass objects.
For (int I = 0; I <5; I ++) // initializes the Array
{
Mc [I] = new MyClass ();
Mc [I]. TheValue = myInt [I];
}
Array. Sort (mc); // an exception is thrown when the Sort method is called.
Foreach (var item in mc)
{
Console. Write ("{0}", item );
Console. Write ("");
}
Console. ReadKey ();
}
}
Class MyClass // custom class MyClass
{
Public int TheValue;
}
The program throws an exception, for example:
Why? The reason why Sort does not work is that for an array of custom objects, it does not know how to compare custom objects and determine their order. It requires the IComparable interface for objects in the array. When running Sort, it calls the CompareTo method of the element and passes in the reference of another element as a parameter to compare one element of the array with another element.
Therefore, we can enable the class to implement the IComparable interface so that the Sort method can be used for MyClass objects.
The Code is as follows:
Class Program
{
Static voidMain (string [] args)
{
Var myInt = new [] {20, 4, 16, 9, 2 };
MyClass [] mc = new MyClass [5]; // creates an array of class MyClass objects.
For (int I = 0; I <5; I ++) // initializes the Array
{
Mc [I] = new MyClass ();
Mc [I]. TheValue = myInt [I];
}
PrintOut ("original array order:", mc); // call the static PrintOut method to output the initial array.
Array. Sort (mc );
PrintOut ("sequence after Sort:", mc); // call the static PrintOut method to output the sorted array.
Console. ReadKey ();
}
Static void PrintOut (string s, MyClass [] mc) // defines the static method PrintOut to traverse the array and output the array elements.
{
Console. Write (s );
Foreach (var item in mc)
{
Console. Write ("{0}", item. TheValue );
Console. Write ("");
}
Console. WriteLine (); // line feed
}
}
Class MyClass: IComparable // class implementation Interface
{
Public int TheValue;
Public int CompareTo (object obj) // implement the interface member Method
{
MyClass mc = (MyClass) obj;
If (this. TheValue <mc. TheValue) return-1;
If (this. TheValue> mc. TheValue) return 1;
Return 0;
}
}
The program output result is:
Implement the IComparable interface in the MyClass class:
3. Declare the interface?
For example:
Interface IMyInterface1 // declare interface IMyInterface1
{
Int Do1 (int I, long l );
Double Do2 (string s, long l );
}
Note the following:
1> declare the interface using the interface keyword.
2> the interface name must start with an uppercase I (for example, ISaveable ).
3> the declaration of interface function members cannot include any implementation code, but the semicolon must be followed by the subject of each member declaration.
4> the interface declaration can have any access modifiers such as public, protected, and private, but the interface members are implicitly public and cannot have any access modifiers, including public.
4. Implementation interface?
Only classes and structures can implement interfaces. To implement interfaces, pay attention to the following points for classes or structures:
1> include the interface name in the base class list.
2> implement each interface member.
3> If the class inherits from the base class and implements the interface, the base class name in the base class list must be placed before any interface.
For example:
Interface IMyInterface1 // declare interface IMyInterface1
{
Int Do1 (int I, long l );
Double Do2 (string s, long l );
}
Class MyClass: IMyInterface1 // implementation Interface
{
Int Do1 (int I, long l) // implementation interface member
{
// Todo: Method body
}
Double Do2 (string s, long l)
{
// Todo: Method body
}
}
5. simple interface Example?
The Code is as follows:
Interface IIfc1 // declare an interface
{
Void PrintOut (string s );
}
Class MyClass: IIfc1 // implementation interface.
{
Public void PrintOut (string s) // implementation interface member
{
Console. WriteLine ("Calling through: {0}", s );
Console. ReadKey ();
}
}
Class Program
{
Static voidMain (string [] args)
{
MyClass mc = new MyClass (); // create an instance
Mc. PrintOut ("object"); // call the Class object method.
}
}
The program output result is:
The above is my summary. I will write it here today. Thank you for your comments!
From: Permanent Wheat
<Script> </script>