1. What is an interface?
A: The interface indicates a group of function members (or methods) without the reference type of the members. This sentence has two meanings.
First, the interface includes a group of function members but is not implemented.
Second, the interface is a reference type.
2. What can an interface do for us?
A: You can use the interfaceImplement Multi-inheritance.Because we can only inherit one base class, but we can inherit multiple interfaces. CLR provides multi-inheritance for the "Scaling version" through interfaces.
1. Define Interfaces
CodeAs follows:
//Interface DefinitionPublic InterfaceImyinterface1 {VoidPrintout (StringS );//Method, the most commonStringName {Get;Set;}//Attribute}
Conclusion: 1. The interface definition uses the keyword interface, and the interface name must start with an uppercase letter I to facilitateSource codeAPI type.
2. The interface definition is similar to the type definition. The interface can be defined within the file range or nested in another type. For example, you can define an interface in a separate CS file.
3. The interface definition can have any access modifier, such as public, protected, internal, or private. However, the interface member cannot contain any modifier because the interface member is implicitly public.
2. Use Interfaces
The following code defines an interface and a class to implement it. The code in main creates class objects and calls implementation methods through class objects. It also creates a variable of the interface type, forcibly converts the Class Object Reference to the interface type reference, and calls the implementation method through the interface reference.
Namespace Interfacedemo1 { // Interface Definition Public Interface Iifc1 { Void Printout ( String S );} // Define class implementation Interface Class Myclass: iifc1 { Public Void Printout ( String S) // Provide implementation for interface methods {Console. writeline ( " Calling through: {0} " , S );}} Class Program { Static Void Main ( String [] ARGs) {myclass MC = New Myclass (); Mc. printout ( " Object " ); // Call implementation methods through class objects // If the class implements the interface, the expression returns a reference pointing to the interface. // If the class does not implement the interface, the expression returns NULL instead of throwing an exception, which is better than using the forced conversion operator. Iifc1 IFC = MC As Iifc1;// There are two methods to obtain interface references. The other is to use the forced conversion operator, for example, iifc1 IFC = (iifc1) MC; If (IFC! = Null ) {IFC. printout ( " Interface " ); // Call implementation method through interface reference } Console. readkey ();}}}
ProgramOutput result:
Iii. Generic Interfaces
First, let's look at the following code:
private void somemethod1 () { int X = 1 , y = 2 ; icomparable C = x; /// Get interface references because int implements the icomparable interface. C. compareto (y); /// Y is boxed here (because compareto expects to accept object parameter types) C. compareto ( " 2 " ); /// after compilation, an error is returned. }
This Code contains the following two problems.
First, it is often boxed when processing value types, resulting in performance loss.
Second, low type security. When calling an interface method, you can pass a reference to any type of instance. This is not what we expect.
The best way to solve these problems is the generic interface. The modified code is as follows:
private void somemethod2 () { int X = 1 , y = 2 ; icomparable int C = x; /// Get interface reference C. compareto (y); /// Y will not be boxed (because compareto expects to accept the int parameter type) C. compareto ( " 2 " ); /// compilation error (because the expected parameter type of compareto is int) ensures type security. }
Therefore, the advantages of generic interfaces are summarized as follows:
1. type security during compilation.
2. When the value type is processed, the number of times of packing is reduced.
3. The class can implement the same interface multiple times, as long as different type parameters are used each time.
4. Design: base class or interface?
Since both the base class and interface can implement inheritance, should we use the base class or interface during design? The following are the guiding principles summarized:
1, IS-A or can-do relationship. First Judge the relationship between the derived class and the base class, if it is the relationship of IS-A (belong), it uses the base class to implement inheritance. If it is a can-do (can do something) relationship, use the interface to implement inheritance.
2. It is easier to derive from the base class. Because the derived classes of all functions provided by the base class only need to make slight changes to their behavior. If an interface is implemented, all its members must be implemented.
3. version control can be better managed by deriving a base class. If you add a method to a type, the derived class directly inherits the new method, and the Code does not need to be re-compiled. When a member is added to an interface, the successor of the interface will force the source code to be modified and re-compiled, which is not conducive to program version control.