C # interface

Source: Internet
Author: User

C#interface (Interface)

The interface defines the syntax contracts that should be followed when all classes inherit an interface. The interface defines the "what" part of the syntax contract, and the derived class defines the "How to" section of the syntax contract.

An interface defines properties, methods, and events, which are members of an interface. The interface contains only the declarations of the members. The definition of a member is the responsibility of the derived class. The interface provides the standard structure that a derived class should follow.

Abstract classes are somewhat similar to interfaces, but most of them are only used when only a few methods are declared by the base class by a derived class.

Interfaces are reference types, similar to classes, and are similar to abstract classes with three points:
1, can not be instantiated;
2, including the non-implementation of the method declaration;
3. Derived classes must implement methods that are not implemented, abstract classes are abstract methods, and interfaces are all members (not just methods including other members);

In addition, interfaces have the following characteristics: Interfaces can contain properties, indexers, events, and those members are defined as public, in addition to methods. In addition, you cannot include any other members, such as constants, fields, constructors, destructors, static members. A class can inherit directly from multiple interfaces, but only a single class (including abstract classes) is inherited directly.

the role of the C # interface

1. An interface is used to describe a common method/public property of a set of classes. It does not implement any method or property, just tells the class that inherits it, at least, what functions to implement, and the classes that inherit it can add their own methods.

2. Using an interface allows you to inherit its class: named Uniform/Specification, easy to maintain. For example: Two classes of "dog" and "cat", if they all inherit the interface "Animal", in which the animal has a method behavior (), then the dog and the cat must implement the behavior () method, And they are all named behavior so that there is no name too messy. If the name is not behavior (), the interface will constrain the naming of the compiler without being constrained by the interface constraint.

3. Provide a permanent interface. When the class is added, the existing interface method can satisfy most of the methods in the inheriting class, there is no need to re-design a set of methods for the new class, save the code and improve the development efficiency.

declaring interfaces

An interface uses the interface keyword declaration, which is similar to a class declaration. The interface declaration is public by default. The following is an instance of an interface declaration:

Common interface: "Animal" public interface IAnimal {        void Behavior ();//behavioral methods that describe the characteristics of various animals}//class: Dog public class dog:ianimal{        pub LIC void Behavior ()        {               //console.write ("I Sleep at night, daytime activities");                MessageBox.Show ("I Sleep at night, daytime activities");         } }//class: Cat public class cat:ianimal{public      void Behavior ()      {                //console.write ("I sleep during the day, evening Activities");                MessageBox.Show ("I sleep during the day, evening Activities");       } }//Simple application: public static Main () {        dog Mydog = new Dog ();        Mydog.behavior (); Output: "I sleep at night, daytime activities"        cat Mycat = new Cat ();        Mycat.behavior (); Output: "I sleep during the day, Evening Activities"}

The above call different classes of the same name method, will output a different east, that is, each class inside the same name method to complete the function can be completely different.

Further, it is not a method of invoking a class using the Main method above, which implements its invocation with polymorphism.

Take a look at the following method:

public void Behavior (IAnimal myianimal) {            myianimal.behavior ();}

Its argument is an interface type , and any class that inherits it can call this method, which can call methods in different classes depending on the class. can also be able to do their own according to different classes, the function of different classes.

Examples of polymorphism codes:

Dog Mydog = new Dog (); Cat Mycat = new Cat (); Behavior (Mydog); Behavior accepts "dog" class instances Behavior (Mycat); Behavior accept "Dog" class instances

This way the behavior method writes one time to complete all the different functions of the same name method in the class that inherits it. Very convenient, again, due to "animal software" functional requirements, need to add a "turtle" class:

Class: Turtle public Tortoise:ianimal {public        void Behavior ()        {                  MessageBox.Show ("I can not live, sleep 5,000 years!");}       }

It is also possible to invoke the above polymorphic method, so that the interface makes the method more extensible. If you inherit a lot of its classes, how many benefits are imaginable! (Source: http://blog.csdn.net/yunhaic/article/details/6662843)

Similarly, suppose our company has two kinds of programmer: VB programmer, refers to the programmer that writes program with VB, uses Clsvbprogramer this class to express, Delphi Programmer refers to the programmer that uses Delphi to write program, use Clsdelphiprogramer this class to express. Each class has a Writecode () method. Defined as follows:

public class clsvbprogramer{public    void Writecode ()    {        //write code in VB language;    }}public class Clsdelphiprogramer {Public    void Writecode ()    {        //write code in Delphi language;    }}public class clsproject{public    void Writeprograme (Clsvbprogramer programer)//VB Write code    {        Programer. Writecode ();    }    public void Writeprograme (Clsdelphiprogramer programer)//Overloaded method, with Delphi write code    {        Programer. Writecode ();    }} static void Main (string[] args) {    clsproject proj = new Clsproject ();    If you need to write code in VB    clsvbprogramer programer1 = new Clsvbprogramer ();    Proj. Writeprograme (programer1);    If you need to write code with Delphi    Clsdelphiprogramer programer2 = new Clsdelphiprogramer ();    Proj. Writeprograme (PROGRAMER2);}

But if the company came to a C # programmer, how do we change the program so that it can implement the function of writing programs in C #? We need to add a new class Clscsharpprogramer, and Clsproject this class again to reload the Writeprograme (Clscsharpprogramer programer) method. It's a lot of trouble. If there are C programmers, C + + programmers, Java programmers. It's too much trouble!

But if you switch to an interface, it's completely different:
First declare a programmer interface:

    Class Program {static void Main (string[] args) {Clsproject proj = new Clsproject ();            Iprogramer Programer;            If you need to write code in VB Programer = new Clsvbprogramer (); Proj.            Writeprograme (Programer);            If you need to write code with Delphi Programer = new Clsdelphiprogramer (); Proj.        Writeprograme (Programer); }}///<summary>//Declaration interface///</summary> public interface Iprogramer {void writecod    E (); }//Then declare the class and implement the Iprogramer Interface:///<summary>//VB Programmer-Writecode Method///</summary> public class CL Svbprogramer:iprogramer {public void Writecode () {//write code in VB language; Console.WriteLine        ("I am the VB Programmer implementation"); }}///<summary>///Delphi Programmer Implementation of Writecode Method///</summary> public class Clsdelphiprogramer:           Iprogramer {public void Writecode () {//write code in Delphi language; Console.WriteLine ("I Am a Delphi programmer"); }}///<summary>//CSharp Programmer implemented Writecode method///</summary> public class Clscsharpprogramer: Iprogramer {public void Writecode () {//write code in Delphi language; Console.WriteLine ("I am CSharp Cheng        "), which the sequencer implements. }}///<summary>//project///</summary> public class Clsproject {public void Writepr Ograme (Iprogramer programer) {programer. Writecode ();//Write Code}}

C # interface

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.