The role of the C # interface

Source: Internet
Author: User

1, the role of C # interface:

C # interface is a lot of beginners in C # is easy to confuse things, it seems very simple, define the interface, contains methods, but there is no way to implement the code, and then inherit the interface of the class to implement the interface of all the methods of code, but did not really realize the role of the interface is superfluous , of course, you think that is absolutely wrong, Bill Gates's Microsoft staff are Bugets also smart people, their C # can add such a lot?! On the role of the interface, online there is a really easy to understand to us to do a good understanding of the analysis.

We define an interface
public interface Ibark
{
void Bark ();
}
Define a class again, inherit from Ibark, and must implement the bark () method in it
public class Dog:ibark
{
Public Dog ()
{

     }
Public void Bark ()
     {
consol.write ("barking");
     }
}
Then, declare an instance of dog and call the bark () method
Dog wong Choy =new Dog ();
Wang Choi. Bark ();
Just imagine, if you want to call the bark () method, only need to declare in the dog () such a method is not OK, what to do with the interface. Because there is no bark () implementation in the interface. Do you want to use an interface other than superfluous?
Others say this: From the interface definition aspect, the interface is actually a kind of contract between class and class, a kind of constraint. Take the example above. All classes that inherit the Ibark interface must implement the bark () method. So from the perspective of the user (who uses the Class), If he knows that a class is inherited from the Ibark interface, then he can confidently call the bark () method without having to control how the bark () method is implemented. For example, we wrote another class.
Public class Cat:ibark
{
Public Cat ()
            {

}
public void Bark ()
{
Consol.write ("Meow meow");
}
}
When users use cat classes or dog classes, knowing that they inherit from Ibark, you can call the bark () method without having to implement the specific implementation in the class, because there is definitely a concrete implementation of the bark () method in these two classes.

If we look at it from the point of view of design. A project with several classes to write, because these classes are more complex, the workload is relatively large, so that each class needs to occupy a staff to write. For example, a programmer to the dog class, b programmer to write cat class. The two classes are not connected. But because users need them to implement a "call" method. It's going to be a constraint on them. Let them all inherit from the Ibark interface, the purpose is to facilitate unified management. The other is convenient to call. Of course, you can do this without the use of an interface. But that is not so obvious. If such a class has duck class, and so on, it is inevitable that some people will miss out on this method. So it is more reliable through the interface, stronger binding.

2, C # interface in layman's:

Through the study of the role of the interface in C # has a further understanding, take out to share with you, have said the wrong place please advise.


Suppose our company has two kinds of programmers: VB programmer, refers to use VB to write program programmer, with Clsvbprogramer This class, Delphi programmer refers to use Delphi to write program programmer, with Clsdelphiprogramer this class to express. Each class has a Writecode () method. Defined as follows:

Class Clsvbprogramer ()
{
....
Writecode ()
{
//write code in VB language;
}
....
}

Class Clsdelphiprogramer ()
{
....
Writecode ()
{
//write code in Delphi language;
}
....
}

Now the company has a project that requires a programmer to write a program.
class Clsproject ()
{
....
writeprograme (Clsvbprogramer programer)//VB Write code
                {
Programer. Writecode ();
                }
writeprograme (Clsdelphiprogramer programer)//heavy-duty method, with Delphi write code
                {
Programer. Writecode ();
                }
                  ......
}
In the main program we can write this:
Main ()
{
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 use an interface, it's completely different:
First declare a programmer interface:
interface iprogramer ()
{
writecode ();
}

Span style= "COLOR: #ff0000" >class clsvbprogramer (): Iprogramer
{
....
writecode ()
{
//write code in VB language;
}
....
}

class clsdelphiprogramer (): Iprogramer
{
....
writecode ()
{
//write code in Delphi language;
}
....
"

Span style= "COLOR: #ff0000" >class clsproject ()
{
....
writeprograme (iprogramer programer)
{
Programer. Writecode ();//write code
}
...
}

Main ()
{
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);
}
If any more programmers like C#,c,c++,java are added, we just need to add their related classes and then make a slight change in main (). Extensibility is particularly good!

In addition, if we clsproject this class into a component, then when our users need to expand the function, we only need to make a small external changes can be achieved, we can say there is no need to change the components we have sealed! is not very convenient, very powerful!

The role of the 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.