First recognized C # interface,

Source: Internet
Author: User

First recognized C # interface,
C # Interface)

The interface defines the syntax contract to be followed when all classes inherit interfaces. Interface defined syntax contract"What is it"Section, the derived class defines the syntax contract"How to do it".

The interface defines attributes, methods, and events. These are all members of the interface. The interface only contains the Declaration of Members. The definition of a member is the responsibility of a derived class. The interface provides the standard structure that the derived class should follow.

Abstract classes are similar to interfaces to some extent. However, they are mostly used only when only a few methods are implemented by the Base class declaration by the derived class.

The interface is of reference type. It is similar to a class and has three similarities with the abstract class:
1. It cannot be instantiated;
2. contains an unimplemented method statement;
3. The derived class must implement unimplemented methods. The abstract class is an abstract method, and the interface is all members (not only the methods include other members );

In addition, interfaces have the following features: In addition to methods, interfaces can also contain attributes, indexers, and events, and these members are defined as common. It cannot contain any other Members, such as constants, fields, constructors, destructor, and static members. A class can directly inherit multiple interfaces, but can only inherit one class (including abstract classes ).

C # functions of interfaces

1. interface is used to describe the public methods/Public attributes of a group of classes. it does not implement any methods or attributes, but only tells the inherited class at least what functions should be implemented. The inherited class can add its own methods.

2. you can use interfaces to make the classes that inherit them: uniform names/specifications, easy to maintain. for example, if two classes of "dogs" and "Cats" inherit the interface "Animals", the animal contains the Behavior () method (), so dogs and cats must implement the Behavior () method and name them Behavior so that the naming will not be too messy. if the name is not Behavior (), the interface will be constrained, that is, the compilation will not pass without the interface constraints.

3. provides permanent interfaces. When classes are added, the existing interface methods can satisfy the majority of methods in the inherited classes. There is no need to re-design a set of methods for the new classes, saving code and improving development efficiency.

Declaration Interface

Interface UsageInterfaceKeyword declaration, which is similar to the declaration of the class. The interface declaration is public by default. The following is an example of an interface declaration:

// Public interface: "animal" public interface IAnimal {void Behavior (); // Behavior method, describing the characteristics of various animals} // class: Dog public class Dog: IAnimal {public void Behavior () {// Console. write ("I am sleeping at night, daytime activity"); MessageBox. show ("sleeping at night, daytime activities") ;}// class: Cat public class Cat: IAnimal {public void Behavior () {// Console. write ("I go to bed during the day, and work at night"); MessageBox. show ("my daytime sleeping, evening activities") ;}// simple application: public static Main () {Dog myDog = new Dog (); myDog. behavior (); // output: "I am sleeping at night, daytime activity" Cat myCat = new Cat (); myCat. behavior (); // output: "I am sleeping during the day, evening activity "}

The above calls the Same Name method of different classes will output different things, that is, the functions of the same name method in each class can be completely different.

Furthermore, instead of calling class methods one by one using the preceding Main method, the method is called using polymorphism.

Let's look at the following method:

public void Behavior(IAnimal myIanimal){            myIanimal.Behavior();}

The parameter isInterface TypeAny class that inherits it can call this method, which can call methods in different classes according to different classes. that is, you can complete the functions of different classes based on different classes.

Sample Code for Polymorphism:

Dog myDog = new Dog (); Cat myCat = new Cat (); Behavior (myDog); // Behavior accepts "Dog" class instance Behavior (myCat ); // Behavior accepts "dog" type instances

In this way, the Behavior method can complete different functions of the same name method in all classes that inherit it. it is very convenient. Similarly, due to the functional requirements of "animal software", another "Turtle" category needs to be added:

// Class: public Tortoise: IAnimal {public void Behavior () {MessageBox. Show ("I can stay up for five thousand years after a sleep! ");}}

You can also call the above multi-state method, so the interface makes the method more extensible. If there are many classes that inherit it, what are the benefits! (Source: http://blog.csdn.net/yunhaic/article/details/6662843)

Similarly, assume that our company has two types of programmers: VB programmers, which refer to programmers who use VB to write programs and are represented by clsVBProgramer; delphi programmers refer to programmers who use Delphi to write programs and use clsDelphiProgramer to represent them. Each class has a WriteCode () method. Definition:

Public class clsVBProgramer {public void WriteCode () {// write code in the VB language;} public class clsDelphiProgramer {public void WriteCode () {// write code in the Delphi language ;}} public class clsProject {public void WritePrograme (clsVBProgramer programer) // use VB to write code {programer. writeCode ();} public void WritePrograme (clsDelphiProgramer programer) // overload method, use Delphi to write code {programer. writeCode () ;}} static void Main (string [] args) {clsProject proj = new clsProject (); // if you need to use VB to write code clsVBProgramer programer1 = new clsVBProgramer (); proj. writePrograme (programer1); // if you need to use Delphi to write code clsDelphiProgramer programer2 = new clsDelphiProgramer (); proj. writePrograme (programer2 );}

However, if the company has another C # programmer, how can we modify this program so that it can implement the function of writing a program using C? We need to add a new clsCSharpProgramer class, and re-load the WritePrograme (clsCSharpProgramer programer) method in this clsProject class. This is a lot of trouble. If there are still C programmers, C ++ programmers, and JAVA programmers. It's too much trouble!

However, if you use an interface, it will be totally different:
First, declare a programmer interface:

Class Program {static void Main (string [] args) {clsProject proj = new clsProject (); IProgramer programer; // if you need to use VB to write code programer = new clsVBProgramer (); proj. writePrograme (programer); // if you need to use Delphi to write code programer = new clsDelphiProgramer (); proj. writePrograme (programer) ;}/// <summary >/// declare the interface /// </summary> public interface IProgramer {void WriteCode ();} // then declare the class and implement the IProgramer interface: // <summary> // WriteCode method implemented by the VB programmer /// </summary> public class clsVBProgramer: IProgramer {public void WriteCode () {// write code in VB; Console. writeLine ("implemented by VB programmers"); }}/// <summary> // WriteCode method implemented by Delphi programmers /// </summary> public class clsDelphiProgramer: IProgramer {public void WriteCode () {// write code in Delphi; Console. writeLine ("implemented by Delphi programmers"); }}/// <summary> // WriteCode method implemented by CSharp programmers /// </summary> public class clsCSharpProgramer: IProgramer {public void WriteCode () {// write code in Delphi; Console. writeLine ("I Am a CSharp programmer ");}} /// <summary> /// project /// </summary> public class clsProject {public void WritePrograme (IProgramer programer) {programer. writeCode (); // write code }}

In this case, if there are other programmers, we only need to add their related classes, and then make some modifications in main. Great scalability!

In addition, if we encapsulate the clsProject class into a component, when our users need to expand the function, we only need to make small modifications externally, it can be said that there is no need to modify the components we have already closed! This is very convenient and powerful! (Source: http://blog.jobbole.com/85751)

This article is purely for personal study. If you have any shortcomings, please kindly advise!(Part of this article is taken from the Internet. If you infringe your rights and interests, please contact me in time. Thank you .)

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.