1.INTERFACE definition structure
1 Interface interface-name [:base-inteface-23 interface 4 }
[Attributes] nature (optional);
[Access-modifiers] access modifier (optional);//public internal private protected protected internal;
Interface-name Interface name (used to start with capital I);
[: Base-inteface-list] Interface base class list (optional);
Interface Body Interface body (cannot have access modifier);//Just the signature of the method and property of the interface;
The actual implementation is written in the class using the interface;
1 // Define an Interface 2 Public Interface istorable 3 {4 void Read ();//cannot have access-modifier 5 void Write (object);//only signature, no specific content 6 }
Class Inheritance Interface
1 Public class document:istorable//Inherit Istorable interface 2 {3 Public void Read () {...}//can have access-modifier 4 Public void Write (object obj) {...}//write specific implementation content 5 // ... 6 }
A class inherits multiple interfaces
1 Public Interfaceistorable2 {3 voidRead ();4 voidWrite (Object);5 }6 7 Interfaceicompressible8 {9 voidCompress ();Ten voiddecompress (); One } A - Public classdocument:istorable, icompressible//Inherit a list of multiple interfaces - { the //Implement Istorable - Public voidRead () {...}//write specific implementation content - Public voidWrite (Objectobj) {...} - //Implement Icompressible + Public voidCompress () {...} - Public voiddecompress () {...} +}
Interface Inheritance Interface
Interfaceicompressible{voidCompress (); voiddecompress ();}Interfaceiloggedcompressible:icompressible//interface iloggedcompressible inherit interface icompressible{ voidlogsavedbytes ();} Public classdocument:iloggedcompressible {//Implement Icompressible Public voidCompress () {...} Public voiddecompress () {...} //Implement Iloggedcompressible Public voidlogsavedbytes () {...} To implement all of the interface members}
Explicitly defined and implicitly defined
1 Interfaceistorable2 {3 voidRead ();4 voidWrite ();5 }6 InterfaceItalk7 {8 voidTalk ();9 voidRead ();Ten } One Public classdocument:istorable, Italk A { - PublicDocument (strings) - {...;} the //Make read virtual is defined as a virtual function - Public Virtual voidRead ()//the Read method for istorable is implicitly defined - {...;} - Public voidWrite () + {...;} - voidItalk.read ()//Italk The Read method for an explicit definition cannot have access adornment implicitly style is public + {...;} A Public voidTalk ()//do not require explicit definition at {...;} -}
Explicitly defined and implicitly defined calls
1 public class document:istorable , Italk
2 {
3 Public voidRead ()4 {...;} ... ..5 voidItalk.read ()6 {...;}7 }8 9Document thedoc =NewDocument ("Test");TenThedoc.read ();//calls an implicitly defined read ()
One AItalk Itdoc = thedoc;//Convert to Interface -Itdoc.read ();//invokes an explicitly defined read ()
C # Interface