Interface (interface) is used to define a protocol for a program. The class or structure that implements the interface is strictly consistent with the definition of the interface. With this agreement, you can throw away the limitations of programming languages (theoretically). Interfaces can inherit from multiple base interfaces, and classes or structs can implement multiple interfaces. An interface can contain methods, properties, events, and indexers. The interface itself does not provide an implementation of the members it defines. The interface specifies only the members that the class or interface that implements the interface must provide.
An interface is like a template that defines the methods that an object must implement in order to allow these methods to be referenced as interface instances. The interface cannot be instantiated. A class can implement multiple interfaces and be indexed through these implemented interfaces. An interface variable can only index an instance of a class that implements the interface. Example:
Interface Imyexample {
String This[int index] {get; set;}
Event EventHandler even;
void Find (int value);
String point {get; set;}
}
public delegate void EventHandler (object sender, Event e);
The interface in the example above contains an index of this, an event even, a method find, and a property point.
Interfaces can support multiple inheritance. As in the following example, the interface "IComboBox" inherits from "ITextBox" and "IListBox" at the same time.
Interface IControl {
void Paint ();
}
Interface Itextbox:icontrol {
void SetText (string text);
}
Interface Ilistbox:icontrol {
void Setitems (string[] items);
}
Interface Icombobox:itextbox, IListBox {}
Classes and structs can instantiate interfaces in multiple instances. As in the following example, the class "EditBox" Inherits the class "Control" while inheriting from "IDataBound" and "IControl".
Interface IDataBound {
void Bind (Binder b);
}
public class Editbox:control, IControl, IDataBound {
public void Paint ();
public void Bind (Binder b) {...}
}
In the above code, the "Paint" method comes from the "IControl" interface, and the "Bind" method is implemented from the "IDataBound" interface as "public" in the "EditBox" class.
Description
1. The interfaces in C # are defined independently of the class. This is in contrast to the C + + model, where interfaces are actually abstract base classes in C + +.
2, interfaces and classes can inherit multiple interfaces.
3, a class can inherit a base class, and an interface cannot inherit a class at all. This model avoids the multiple inheritance problem of C + +, and the implementation of different base classes in C + + may conflict. Therefore, complex mechanisms such as virtual inheritance and explicit scopes are no longer needed. The simplified interface model of C # helps accelerate application development.
4. An interface defines a reference type that has only abstract members. What an interface in C # actually does, there is only a method flag, but there is no execution code at all. This implies that an interface cannot be instantiated, and only one object derived from that interface can be instantiated.
5. Interfaces can define methods, properties, and indexes. So, in contrast to a class, the particularity of an interface is that when a class is defined, it can be derived from a multiple interface, and you can only derive from one of the only classes.
Interfaces and Components
The interface describes the services that the component provides externally. Interacts between components and components, between components and customers through interfaces. So once a component is released, it can only provide a reasonable and consistent service through a pre-defined interface. The stability of this interface definition allows the customer application developer to construct a robust application. A component can implement multiple component interfaces, and a specific component interface can be implemented by multiple components.
The component interface must be self-describing. This means that the component interface should not rely on the specific implementation, the implementation and interface separation completely eliminate the interface between the user and the interface of the implementation of the coupling between the users, enhance the packaging of information. This also requires that the component interface must use a language that is not related to the implementation of the component. The current description of the component interface is the IDL language.
Because the interface is a protocol between components, once the interface of the component is published, the component producer should keep the interface as constant as possible, and any changes to the interface syntax or semantics may result in a compromise between the existing component and the customer.
Each component is autonomous, has its own unique function, and can only communicate with the outside world through the interface. When a component needs to provide new services, it can be implemented by adding new interfaces. Does not affect customers that already exist on the original interface. And new customers can re-select the new interface to get the service.
Component-oriented programming
The component-based programming method inherits and develops the object-oriented program design method. It applies object technology to system design, and further abstracts the realization process of object-oriented program design. We can use the component-based programming method as a method to construct the architecture hierarchy of the system, and the object-oriented method can be used to implement the component conveniently.
Component-oriented programming emphasizes true software reusability and high degree of interoperability. It focuses on the generation and assembly of components, which together form the core of the component design. The production process of components is not only the demand of application system, but also the component market, which promotes the development of components and facilitates the communication and cooperation between software vendors. The assembly of components enables software products to be built quickly, similar to building blocks, not only to shorten the development cycle of software products, but also to improve the stability and reliability of the system.
The methods of component programming are characterized by the following aspects:
1. The independence of programming language and development environment;
2, the component position transparency;
3, the process of component transparency;
4, extensibility;
5, reusable;
6, with a strong infrastructure;
7, the system level of public services;
The C # language is well suited for component programming because of its many advantages. This is not to say that C # is a component programming language, nor does C # provide a tool for component programming. We have pointed out many times that components should have features that are not related to programming languages. Readers should keep this in mind: The component model is a specification that must be respected regardless of the programming language in which the component is designed. such as the Assembly of computer examples, as long as the various manufacturers to provide us with the accessories specifications, interfaces consistent with the standard, these accessories together can work together, component programming is the same. We're just saying that using the C # Language for component programming will bring us much more convenience.
Now that you know what an interface is, here's how to define the interface, see the next section-Define the interface.
C # Interface Overview