Define an interface. Define an interface member

Source: Internet
Author: User
Tags uppercase letter
Section 2 Interface Definition

 

Technically, an interface is a set of data structures that contain functional methods. Through this set of data structures, the customerCodeYou 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.

Section 3 defines interface members

An interface can contain one or more members. These members can be methods, attributes, index indicators, and events, but cannot be constants, fields, operators, constructors, or destructor, and cannot contain any static members. The interface definition creates a new definition space, and the interface definition directly includes the interface member definition to introduce the new member into the definition space.

Note:

1. The interface member is a member inherited from the basic interface and a member defined by the interface itself.

2. The interface definition can be zero or multiple members. The interface must be a method, attribute, event, or indexer. An interface cannot contain constants, fields, operators, instance constructors, destructor, or types, nor static members of any type.

3. Define an interface that contains a method, attribute, event, and indexer for each possible type of member.

4. The default access method for interface members is public. The interface member definition cannot contain any modifier. For example, the abstract, public, protected, internal, Private, virtual, override, or static modifier cannot be added before the member definition.

5. The interface members cannot have the same names. The inherited members do not need to be defined, but the interface can define members with the same name as the inherited members. In this case, we say that the interface members overwrite the inherited members, which will not cause errors, but the compiler will give a warning. To disable the warning prompt, add a new keyword before the member definition. However, if the parent interface member is not overwritten, using the new keyword will cause the compiler to issue a warning.

6. The method name must be different from the names of all attributes and events defined in the same interface. In addition, the method signature must be different from the signature of all other methods defined in the same interface.

7. The attribute or event name must be different from the name of all other Members defined in the same interface.

8. The signature of one indexer must be different from that of all other indexers defined in the same interface.

9. attributes in the interface method declaration, return-type, identifier, and formal-parameter-lis) it has the same meaning as those in the method declaration of a class. An interface method declaration does not allow a method subject to be specified, but usually ends with a semicolon.

10. The access character of the interface attribute declaration corresponds to the access character of the Class Attribute declaration, except that the access character subject must usually use a semicolon. Therefore, whether the attribute is read/write, read-only, or write-only, the access character is completely determined.

11. attributes, types, and formal-parameter-list in the interface index Declaration have the same meaning as those declared by the class index.

In the following example, the interface imytest contains the index indicator, event E, method F, and attribute P:

Interface imytest {
String This [int Index] {Get; set ;}
Event eventhandler E;
Void F (INT value );
String P {Get; set ;}
}
Public Delegate void eventhandler (Object sender, eventargs E );

In the following example, the istringlist Interface contains an interface for each possible type of members: A method, an attribute, an event, and an index.

Public Delegate void stringlistevent (istringlist sender );
Public interface istringlist
{
Void add (string S );
Int count {Get ;}
Event stringlistevent changed;
String This [int Index] {Get; set ;}
}

Full name of the interface member

You can also use fully qualified name ). The full name of the interface is composed of this. The interface name is followed by the dot "." And the member name, for example, for the following two interfaces:

Interface iControl {
Void paint ();
}
Interface itextbox: iControl {
Void gettext (string text );
}

Here, the full name of paint is iControl. paint, and the full name of gettext is itextbox. gettext. Of course, the member names in the full name must have been defined in the interface. For example, it is unreasonable to use itextbox. Paint.

If an interface is a namespace Member, its full name must also contain the namespace name.

Namespace System
{
Public interface idatatable {
Object clone ();
}
}

The full name of the clone method is system. idatatable. Clone.

After the interface is defined, the next step is how to access the interface. Please refer to the next section -- access 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.