C # in-depth understanding of the functions of interfaces,

Source: Internet
Author: User

C # in-depth understanding of the functions of interfaces,

 

1. Functions of the C # interface:

The C # interface is something that makes C # beginners easy to confuse. It seems very simple to use. It defines the interface and contains methods, but there is no code to implement the method, then, when the Code of all methods of the interface must be implemented in the class that inherits the interface, but does not really realize the function of the interface, I feel that using the interface is an extra move, of course, it is absolutely wrong to think like this. The employees invited by Bill Gates at Microsoft are still smarter than gates. Can their C # support such a great deal ?! As for the functions of interfaces, one of them on the internet is actually a simple analysis that gives us a good understanding.

We define an Interface

public interface IBark{    void Bark();}

 

Defines a class that inherits from IBark and must implement the Bark () method.

Public class Dog: IBark {public Dog () {} public void Bark () {Console. write ("Wang ");}}

 

Then, declare an instance of Dog and call the Bark () method.

Dog wangcai = new Dog (); wangcai. Bark ();

 

Just imagine, if you want to call the Bark () method, you just need to declare such a method in Dog (). What should you do with interfaces. because the interface does not have the specific implementation of Bark. the implementation must be in Dog. so isn't the interface used here all the more?
Some people say this: in terms of Interface Definition, interfaces are actually a protocol and a constraint between classes. take the above example as an example. all classes that inherit the IBark interface must implement the Bark () method. from the perspective of a user (a user using a class), if he knows that a class inherits from the IBark interface, he can call the Bark () method with confidence, instead of how the Bark () method is implemented.

For example, we have another class.

Public class Cat: IBark {public Cat () {} public void Bark () {Console. write (" ");}}

 

When users use Cat or Dog classes and know that they inherit from IBark, they can directly call the Bark () method without the specific implementation in the class, because the two classes certainly have specific implementations of the Bark () method.

From the design point of view. several Classes need to be written in a project. Because these classes are complex and have a large workload, each class requires one staff member to write. for example, A programmer decides the Dog class, and B programmer writes the Cat class. these two classes are unrelated, but because users need to implement a method called. this requires a constraint on them. they all inherit from the IBark interface to facilitate unified management. the other is convenient to call. of course, you can achieve the same purpose without using interfaces. in this case, such constraints are not so obvious. If such classes have Duck classes and so on, it is inevitable that some people will miss this method when there are many such classes. therefore, the interface is more reliable and more binding.

 

 

2. Introduction to interfaces in C:

I have learned more about the functions of the interfaces in C # And shared them with you. Please advise me if something is wrong.

Assume that our company has two types of programmers: VB programmers, which refer to programmers who write programs using VB, and clsVBProgramer programmers. Delphi programmers refer to programmers who write programs using Delphi, it is represented by the clsDelphiProgramer class. Each class has a WriteCode () method. Definition:

Class clsVBProgramer (){.... writeCode () {// write code in VB ;}....} class clsDelphiProgramer (){.... writeCode () {// write code in Delphi ;}....}

 

Now the company has a project that requires a programmer to write a program.

Class clsProject (){.... writePrograme (clsVBProgramer programer) // use VB to write code {programer. writeCode ();} WritePrograme (clsDelphiProgramer programer) // reload the method and use Delphi to write the code {programer. writeCode ();}....}

 

In the main program, we can write as follows:

Main () {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!

 

But if you use an interface, it will be completely different. First, declare a programmer interface:

Interface IProgramer () {WriteCode ();} // declare two classes and implement the IProgramer interface: class clsVBProgramer (): IProgramer {.... writeCode () {// write code in VB ;}....} class clsDelphiProgramer (): IProgramer {.... writeCode () {// write code in Delphi ;}....}

 

Modify the clsProject class as follows:

Class clsProject () {... WritePrograme (IProgramer programer) {programer. WriteCode (); // write code }....}

 

In the main program, we write as follows:

Main () {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 );}

 

If programmers such as C #, C, C ++, and JAVA are added, we only need to add their related classes, and then add them in main () and then click OK. 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! Is it very convenient and powerful!

 

Source: http://www.cnblogs.com/zhijianliutang/archive/2011/11/16/2250741.html

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.