One of the basic C #-based interface tutorials

Source: Internet
Author: User

Section 1 interface comment

An interface is a protocol used to define a program. The class or structure that implements the interface must be strictly consistent with the interface definition. With this protocol, you can leave the restrictions of programming languages away (theoretically ). Interfaces can be inherited from multiple basic interfaces, while classes or structures can implement multiple interfaces. The interface can contain methods, attributes, events, and indexers. The interface itself does not provide the Implementation of the members it defines. The interface only specifies the class that implements the interface or the members that the interface must provide.

An interface is better than a template. This template defines the methods that must be implemented by the object, so that these methods can be referenced as interface instances. The interface cannot be instantiated. Class can implement multiple interfaces and are indexed through these interfaces. Interface variables can only be indexed to instances of classes that implement this 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 preceding example contains an index this, an event Even, a method Find, and an attribute Point.

The interface supports multiple inheritance. 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 structures can be instantiated multiple times. In the following example, the class "EditBox" inherits the class "Control" and "IDataBound" and "IControl" at the same time.

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; the "Bind" method comes from the "IDataBound" interface, all are implemented in the "EditBox" class as "public.

Note:

1. interfaces in C # are defined independently of classes. This is opposite to the C ++ model. In C ++, interfaces are actually abstract base classes.

2. interfaces and classes can inherit multiple interfaces.

3. The class can inherit a base class, and the interface cannot inherit the class at all. This model avoids the multi-Inheritance Problem of C ++, and implementations in different C ++ classes may conflict with each other. Therefore, complex mechanisms such as virtual inheritance and explicit scopes are no longer needed. C #'s simplified interface model helps accelerate application development.

4. An interface defines a reference type with only abstract members. In C #, what an interface actually does is only the method flag, but no code is executed at all. This implies that one interface cannot be instantiated and only one object derived from this interface can be instantiated.

5. interfaces can define methods, attributes, and indexes. Therefore, to compare a class, the interface is special: when defining a class, it can be derived from multiple interfaces, and you can only derive from only one class.

Interfaces and components

Interface describes the external services provided by the component. Interfaces are used to interact between components and between components, and between components and customers. Therefore, once a component is released, it can only provide reasonable and consistent services through pre-defined interfaces. The stability between these interface definitions enables the customer's application developers to construct robust applications. One component can implement multiple component interfaces, and a specific component interface can also 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 separation of implementation and interface completely eliminates the coupling between the interface user and the interface real-time, and enhances the information encapsulation degree. This also requires that the component interface must use a language unrelated to the component implementation. Currently, the description standard for component interfaces is the IDL Language.

Since interfaces are protocols between components, once a component interface is released, the component producer should try its best to keep the interface unchanged and make any changes to the interface syntax or semantics, may cause the connection between existing components and customers to be damaged.

Each component is independent and has its unique functions. It can only communicate with the outside world through interfaces. When a component needs to provide new services, it can be implemented by adding new interfaces. The existing customers of the original interface are not affected. New customers can select new interfaces to obtain services.

Componentized Program Design

Componentized programming methods inherit and develop object-oriented programming methods. It applies object technology to system design and further abstracts the implementation process of object-oriented programming. The componentized program design method can be used to construct the system architecture hierarchy, and the object-oriented method can be used to conveniently implement components.

Componentized Program Design emphasizes real software reusability and high interoperability. It focuses on Component generation and assembly, which constitute the core of componentized programming. The component generation process is not only the requirement of the application system, but also the component market itself promotes the development of components and the exchange and cooperation of software vendors. Component Assembly allows software products to be quickly established using a method similar to building blocks, which not only shortens the development cycle of software products, but also improves system stability and reliability.

The component programming method has the following features:

1. Programming Language and development environment independence;

2. Transparency of component positions;

3. process transparency of components;

4. scalability;

5. reusability;

6. Strong infrastructure;

7. Public services at the system level;

C # language is suitable for component programming due to its many advantages. However, this does not mean that C # is a component programming language, nor that C # provides a component programming tool. We have already pointed out that components should have characteristics unrelated to programming languages. Keep this in mind: component models are a specification that must be followed no matter which programming language components are used. For example, in the case of computer assembly, as long as the accessory specifications and interfaces provided by various manufacturers meet the unified standards, these accessories can work together, and the same is true for component programming. We just said that using the C # language for component programming will bring us greater convenience.

Now that you know what interfaces are and how to define interfaces, see the next section-define interfaces.

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.