What is an interface?
interface, which is a reference type that specifies a set of function members without implementing them (so the interface can only be implemented with classes and structs).
Declaration of the interface
The syntax for declaring an interface is as follows (by standard, the interface name starts with the uppercase ' I '):
1 Interface Iinterfacename 2 {3 /* member functions 4 func1 (...); 5 Func2 (...); 6 ..... 7 */ 8 }
Notable points:
1) An interface declaration cannot contain data members and static members . Non-static members can only contain methods , properties , events , and indexers .
2) The declaration of an interface can have any access modifiers, such as public, private, and so on. However, members within an interface are implicitlypublic, and members within an interface cannot explicitly indicate any access modifiers.
Implementation of the interface
The syntax for implementing an interface is as follows:
1 //declaring Interfaces2 InterfaceIPrint3 {4 voidPrintmessage (stringmsg);//methods not implemented in interfaces5 }6 7 //classes that implement interfaces8 classMyclass:iprint9 {Ten //method of class self-bringing One voidMyFunc () A { -Console.WriteLine ("This is my own function."); - } the - //ways to implement an interface - voidPrintmessage (stringmsg) - { +Console.WriteLine ("Message is: {0}", msg); - } + } A at class Program - { - Static voidMain () - { -MyClass MC =NewMyClass (); -Mc.printmessage ("I implemented an interface"); in } -}
We can also invoke the implementation method by reference to the interface, with the following syntax:
// converts a reference to a class object to a reference iprt.printmessage ("Print a msg ") of an interface type. " // invoking a reference method
or use the AS operator (if the conversion fails the AS operator returns a null reference):
as // using the as transform if NULL // don't forget to determine if the reference to P is empty. P.printmessage ("Some msg. "
Note: If a class inherits and implements an interface from a base class, the base class name in the base class list of the class must precede all interfaces.
Class can implement multiple interfaces
Classes can implement methods of multiple interfaces, and if some interfaces have the same signature and members of the same return type , then the class can implement a single member to satisfy all interfaces that contain duplicate members, with the following code:
InterfaceIIFC1{ voidPrintOut (strings);} InterfaceIIFC2{ voidPrintOut (stringt);} classMYCLASS2:IIFC1,IIFC2{ Public voidPrintOut (stringS//a single implementation of two interfaces {Console.WriteLine ("This is a msg: {0}", s); }}
Of course, in some cases, you might want to have a separate implementation for each interface, and you can specify an explicit interface member.
classmyclass2:iifc1,iifc2{ Public voidIifc1.printout (stringS//IIFC1 implementation of explicit interface members{Console.WriteLine ("This is a IIfc1 ' s msg: {0}", s); } Public voidIifc2.printout (stringS//IIFC2 implementation of explicit interface members{Console.WriteLine ("This is a IIfc2 ' s msg: {0}", s); }}//It is then called in the Main method.classprogram{Static voidMain () {MyClass2 MC2=NewMyClass2 (); IIFC1 IFC1=(IIFC1) mc2; Ifc1. PrintOut ("Interface 1"); IIFC2 IFC2=(IIFC2) mc2; Ifc2. PrintOut ("Interface 1"); }}
It is important to note that in the same class, other members do not have direct access to explicit interface members:
classmyclass3:iifc1{voidIifc1.printout (strings) {Console.WriteLine ("IIfc1: {0}", s); } voidMyFunc () {PrintOut ("Fail.");//compilation error, failed This. PrintOut ("Fail.");//compilation error, failed((IIFC1) This). PrintOut ("Success.");//Successful Call Method }}
Derived members as implementations
A class that implements an interface can inherit code that is implemented in a base class, even if the derived class does not contain any members:
Interfaceiifc1{voidPrintOut (strings);}classBaseclass:iifc1//base class{ Public voidPrintOut (stringS//ways to implement an interface{Console.WriteLine ("A message: {0}", s); }}classDerivedclass:baseclass, IIFC1//Derived Classes{}classprogram{Static voidMain () {derivedclass DC=NewDerivedClass (); dc. PrintOut ("success!"); }}
Interfaces can inherit interfaces
The interface itself can inherit from one or more interfaces:
Interfaceiifc1{voidfunc1 ();}Interfaceiifc2{voidFunc2 ();}Interfaceiifc3:iifc1,iifc2{}classmyclass4:iifc3{ Public voidfunc1 () {//the realization of func1 } Public voidFunc2 () {//the realization of FUNC2 }}
An interface that inherits from the base interface contains all the interfaces it declares and the members of all the base interfaces .
Resources
C # Illustrated Tutorial-fourth edition Author "Beauty" Daniel M.solis
C # Learning (ii): interfaces