C #-based interface basic tutorial II

Source: Internet
Author: User

Section 2 Interface Definition

Technically, an interface is a set of data structures that contain functional methods. Through this set of data structures, the customer code can call the functions of component objects.

The interface is defined in the following format:


[Attributes] [modifiers] interface identifier [: base-list] {interface-body} [;]

Note:

1. attributes (optional): additional definition information.

2. modifiers (optional): allowed modifiers include new and four access modifiers. New, public, protected, internal, and private. In an interface definition, the same modifier is not allowed to appear multiple times. The new modifier can only appear in the nested interface, indicating that the inherited members with the same name are overwritten. The public, protected, internal, and private modifiers define access permissions for interfaces.

3. Indicators and events.

4. identifier: interface name.

5. base-list (optional): contains a list of one or more explicit base interfaces separated by commas.

6. interface-body: defines interface members.

7. An interface can be a member of a namespace or class and contain the signature of the following members: method, attribute, and indexer.

8. An interface can be inherited from one or more basic interfaces.

The interface concept is very similar in C # and Java. The key word of an interface is interface. An interface can expand one or more other interfaces. By convention, the interface name starts with an uppercase letter "I. The following code is an example of the C # interface, which is exactly the same as the interface in Java:


Interface IShape {
Void Draw ();
}

If you derive from two or more interfaces, the names of the parent interfaces are separated by commas, as shown in the following code:


Interface INewInterface: IParent1, IParent2 {}

However, unlike Java, interfaces in C # cannot contain fields ). In addition, in C #, all methods in the interface are public methods by default. In Java, a method definition can carry a public modifier (even if this is not necessary), but in C #, explicitly specifying a public modifier for an interface method is invalid. For example, the following C # interface will generate a compilation error.


Interface IShape {public void Draw ();}

The following example defines an interface named IControl, which contains a member method "Paint:


Interface IControl {
Void Paint ();
}

In the following example, the interface IInterface inherits from two basic interfaces, IBase1 and IBase2:


Interface IInterface: IBase1, IBase2 {
Void Method1 ();
Void Method2 ();
}

Interfaces can be implemented by classes. The identifier of the implemented interface appears in the base list of the class. For example:


Class Class1: Iface1, Iface2 {
// Class member.
}

When the base list of a class includes both the base class and interface, the base class appears first in the list. For example:


Class ClassA: BaseClass, Iface1, Iface2 {
// Class member.
}

The following code snippet defines the interface IFace, which has only one method:


Interface IFace {
Void ShowMyFace ();
}

An object cannot be instantiated from this definition, but a class can be derived from it. Therefore, this class must implement the ShowMyFace abstract method:


Class CFace: IFace
{
Public void ShowMyFace (){
Console. WriteLine ("implementation ");
}
}
Basic Interface

An interface can be inherited from zero or multiple interfaces, which are the explicit basic interfaces called this interface. When an interface has more than zero explicit basic interfaces, the form in the interface definition is that the interface identifier is followed by a colon ":" and a comma ", "List of separated basic interface identifiers.

Interface base:

: Interface type list description:

1. An explicit base interface of an interface must be at least as accessible as the interface itself. For example, it is incorrect to specify a private or internal interface in the basic interface of a public interface.

2. It is wrong to inherit an interface from it directly or indirectly.

3. The base interfaces of interfaces are both explicit base interfaces and their base interfaces. In other words, the set of basic interfaces is completely composed of the explicit basic interfaces and their explicit basic interfaces. In the following example


Interface IControl {
Void Paint ();
}
Interface ITextBox: IControl {
Void SetText (string text );
}
Interface IListBox: IControl {
Void SetItems (string [] items );
}
Interface IComboBox: ITextBox, IListBox {}

The basic interfaces of IComboBox are IControl, ITextBox, and IlistBox.

4. An interface inherits all the members of its basic interface. In other words, the above interface IComboBox inherits the SetText and SetItems members just like painting.

5. A class or structure that implements interfaces also implicitly implements the basic interfaces of all interfaces.

Interface subject

The interface subject of an interface defines the interface members.


Interface-body:
{Interface-member-declarationsopt}

Defining an interface is mainly an interface member. Please refer to the next section-defining an interface member.

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.