Two-based interface basic tutorial

xiaoxiao2021-03-06  45

Second section definition interface

Technically, the interface is a set of data structures containing the function type method. With this set of data structures, customer code can call the functionality of the component object.

The general form of defining the interface is:

[attributes] [Modifiers] Interface Identifier [: Base-list] {interface-body} [;]

Description:

1, Attributes: Additional definition information.

2, Modifiers: Allows the modifiers that are used with NEW and four access modifiers. They are: New, Public, Protected, Internal, Private. In an interface definition, the same modifier does not allow multiple times, the New modifier can only appear in the nested interface, indicating that the same name member of the inheritance comes. The public, protected, both, and private modifiers define access to the interface.

Virtual host

3, indicators and events.

4, Identifier: Interface name.

5, base-list: Contains a list of one or more explicit base ports, and the interface is separated by a comma.

6, Interface-Body: Definition of the interface member.

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

8, one interface can be inherited from one or more base ports.

The concept is very similar in C # and Java. The keywords for the interface are Interface, and an interface can extends one or more other interfaces. According to the practice, the name of the interface starts with uppercase letters "i". The following code is an example of a 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 name list of the parent interface is separated by commas, as shown in the following code:

Interface inewinterface: iParent1, iParent2 {}

However, unlike Java, the interface in the C # cannot contain a domain (Field). Also note that in C #, all methods in the interface are common methods by default. In Java, the method definition can carry a public modifier (even if it is not necessary), but in C #, the method explicitly interfaces to the interface specifies that the public modifier is illegal. For example, the following C # interface will generate a compilation error.

Virtual host

Interface ishape {public void Draw ();

The following example defines an interface called IControl, and includes a member method PAINT:

Interface icontrol {

Void paint ();

}

In the following example, interface IINTERFACE inherits from two base ports ibase1 and ibase2:

Interface IINTERFACE: IBASE1, IBASE2 {

Void method1 ();

Void method2 ();

}

The interface can be implemented by the class. The identifier of the implemented interface appears in the base list of the class. E.g:

Class class1: iface1, ifce2 {

// Class member.

}

When the class list is included, the base class is first appeared when the base class and the interface are included. E.g:

Class Classa: Baseclass, IFACE1, IFACE2 {

// Class member.

}

The following code segment defines the interface ifce, it only has a method: interface ifce {

Void ShowmyFace ();

}

You cannot instantiate an object from this definition, but you can derive a class from it. Therefore, this class must implement the ShowmyFace abstract method:

Class CFace: Iface

{

Public void showmyface () {

Console.writeLine ("Implementation");

}

}

Basegade

An interface can be inherited from zero or multiple interfaces, those explicit base connectors called this interface. When an interface is more than zero-multi-expirant base, then in the form of the interface is in the form of the interface identifier, the interface identifier is followed by a colon ":" and a comma "," separated base interface identifier list.

Interface base:

: Interface Type List Description:

1. The explicit base reference of an interface must be at least accessible to the interface itself. For example, specifying a private or internal interface in the base interface of a common interface is wrong.

2, one interface is inherited directly or indirectly is wrong.

3. The base reference of the interface is an explicit base interface and is the base reference thereof. In other words, the set of basegings is completely composed of explicit base interfaces and their explicit base interfaces. In the example below

Interface icontrol {

Void paint ();

}

Interface ITextBox: icontrol {

Void setText (String text);

}

Interface ilistbox: icontrol {

Void setItems;

}

Interface ICOBOX: ITEXTBOX, IListBox {}

ICOMBOBOX basegings are iControl, ITextBox, and IListbox.

4. One interface inherits all members of its base connection. In other words, the above interface ICOMBOBOX inherits the member setText and SetItems as Paint.

5. A class or structure that implements an interface also implies the base interface of all interfaces.

Interface main body

An interface interface body defines a member of the interface.

Interface-body:

{Interface-Member-Declarationsopt}

The definition interface is primarily defined as a member, please see the next section - Define the interface member.

转载请注明原文地址:https://www.9cbs.com/read-73250.html

New Post(0)