C # Learning (iv) interface

Source: Internet
Author: User
Tags modifiers

The four trouble teacher in detail in C # about interfaces, arrays and strings of knowledge, because the latter two knowledge points too fragmented, not easy to record, so this study record only discuss the interface.

interface , which is declared using the interface keyword, is a function of combining the required members to encapsulate a set of functions. It is like a template in which you define the members that an object must implement, and implement it through a class or struct.

It is necessary to keep in mind that an interface cannot be instantiated directly, that an interface cannot contain any code for a member, but that the member itself is defined. The specific code for the interface member is provided by the class that implements the interface.

The declaration format of the interface is as shown in the following code:

1 Interface interface-name [:base-inteface-23         Interface4 }


Various interpretations

attributes (optional): additional defined information.

Access-modifiers (optional): The modifier that is allowed to be used has a new and four access modifiers. respectively:new, public,protected,internal, private. The same modifier is not allowed to appear more than once in an interface definition, and the new modifier can only appear in a nested interface, overwriting the inherited member with the same name. Public, protected, internal, private modifiers define access to the interface, and the default value is internal.

interface-name : Interface name, which is used to start with capital I.

base-inteface-list(optional): contains one or more lists of explicit base interfaces, separated by commas between the interfaces.

Interface body : interface Body (cannot have access modifier), just define the method and properties of the interface, etc., the actual implementation is written in the class using the interface.

The actual interface declaration code example is as follows:

1  Public Interface istorable 2 {3     void Read ();//Note there must be no modifiers like public. 4     void Write (object);//And only signature, cannot have actual content 5 }

Examples of inheritance use of interfaces are as follows:

1  Public class document:istorable//such as inheriting general class general inheritance interface 2 {3      Public void Read () {...} Here you can add modifiers and write implementation code 4public     void Write (object  obj) { ...} 5     //  ... 6 }

A class can inherit multiple interfaces, and the code example is as follows:

1 Interfaceicompressible//defining a new interface2 {3     voidCompress ();4     voiddecompress ();5 }
6 Public classdocument:istorable, Icompressible7 {8 //Implement Istorable9 Public voidRead () {...}Ten Public voidWrite (Objectobj) {...} One //Implement Icompressible A Public voidCompress () {...} - Public voiddecompress () {...} -}

In addition, the interface can inherit the interface, the code example is as follows:

1 Interfaceiloggedcompressible2 : Icompressible3 {4     voidlogsavedbytes ();5 }6 7  Public classdocument:iloggedcompressible8 {9     //Implement IcompressibleTen      Public voidCompress () {...} One      Public voiddecompress () {...} A     //Implement Iloggedcompressible -      Public voidlogsavedbytes () {...} -}

Interface inheritance interfaces do not implement methods in an inherited interface, but when a class inherits an interface that inherits other interfaces, it is important to implement all the methods in all interfaces on the inheritance chain, or it will error.

At the same time, the interface can inherit multiple interfaces simultaneously, the code example is as follows:

1 Interface  2     : istorable, iloggedcompressible//simultaneously inherits two interfaces 3{4     void logoriginalsize ();  // new ways to add 5 }

C # Learning (iv) interface

Related Article

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.