1, the role of C # interface
Use generalization: Define the interface, which contains the method, but there is no way to implement the code specifically. Then the code that implements all the methods of the interface in the class that inherits the interface. But without really recognizing the role of the interface is superfluous, of course, you think that is absolutely wrong. On the role of the interface, online there is a really easy to understand to us to do a good understanding of the analysis.
[CSharp] View plain copy
<p></p><pre code_snippet_id= "219991" snippet_file_name= "blog_20140306_1_6634576" name= "code" class= "CSharp" >//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 () //Implement Bark () method { Consol.write ("barking"); } }
Then, declare an instance of dog and call the bark () method
Dog Wong Choy =new dog ();
Wong Choy. Bark ();</pre>
Question: If you want to call the bark () method, simply declare a method in the dog () is not the only way to do it, and 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 the control bark () method is specifically how to implement the < For example, we wrote a different class. When the user uses cat or dog classes, Knowing that they inherit from the Ibark, then you can call the bark () method without having to do with the specific implementation in the class, because there must be 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.
The simplification of interfaces in C #
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:
[CSharp] View plain copy
Class Clsvbprogramer () { .... Writecode () { //write code in VB language; } Class Clsdelphiprogramer () { .... Writecode () { //write code in Delphi language; }
/* Now the company has a project to send a programmer to write a program */
Class Clsproject () { .... Writeprograme (Clsvbprogramer programer)//VB Write code { Programer. Writecode (); } Writeprograme (Clsdelphiprogramer programer)//Overloaded method, with Delphi write code { Programer. Writecode (); } ...... }
In the main program we can write this:
Main () { clsproject proj=new clsproject; If need to write code with 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:
Interface Iprogramer () {Writecode ();}
Then declare two classes and implement the Iprogramer interface:
Class Clsvbprogramer (): iprogramer{.... Writecode () { //write code in VB language;} Class Clsdelphiprogramer (): iprogramer{.... Writecode () { //write code in Delphi language;}
Make a change to the Clsproject class:
Class Clsproject () {.... Writeprograme (Iprogramer programer) { programer. Writecode ();//Write Code} ...} Main () { clsproject proj=new clsproject; Iprogramer Programer; If need to write code with VB programer=new Clsvbprogramer; Proj. Writeprograme (programer); If need to use Delphi write code 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!