C # Interface

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.