Home  >  Article  >  Backend Development  >  Detailed explanation of examples of C# interface

Detailed explanation of examples of C# interface

Y2J
Y2JOriginal
2017-04-24 13:36:074577browse

1. The role of C# interface

Usage summary: Define an interface, which contains methods, but there is no code for the specific implementation of the methods. Then in the class that inherits the interface, you need to implement the code for all methods of the interface. But when you don't really understand the role of interfaces, you feel that using interfaces is superfluous. Of course, you are absolutely wrong to think so. Regarding the role of interfaces, there is someone on the Internet who gave us a very understandable analysis in a simple and in-depth manner.

[csharp] view plain copy

<p></p><pre code_snippet_id="219991" snippet_file_name="blog_20140306_1_6634576" name="code" class="csharp">//我们定义一个接口  
public interface IBark    
{  
    void Bark();  
}

//Define another class, inherit from IBark, and must implement the Bark() method

public class Dog:IBark    
{  
    public Dog()  
    {}  
    public void Bark()  //实现其中的Bark()方法  
    {  
       Consol.write("汪汪");  
     }  
}

//Then, Declare an instance of Dog and call the Bark() method

Dog Wangcai=new Dog();

Wangcai.Bark();bc5574f69a0cba105bc93bd3dc13c4ec

Question: If you want to call the Bark() method, don't you just need to declare such a method in Dog()? Why use an interface? Because there is no specific implementation of Bark() in the interface. The real implementation is still In Dog(). So isn’t it unnecessary to use an interface?

Some people also say this: From the definition of interface, the interface is actually an agreement between classes, a kind of Constraints. Take the above example as well. All classes that inherit the IBark interface must implement the Bark() method. So from the perspective of the user (the user who uses the class), if he knows that a certain class inherits from IBark interface, then he can confidently call the Bark() method without worrying about how the Bark() method is implemented. For example, we wrote another class. When the user uses the Cat class or the Dog class When you know that they inherit from IBark, you can directly call the Bark() method without worrying about the specific implementation in the class, because there must be specific implementations of the Bark() method in these two classes.

If we look at it from a design point of view, several classes need to be written in a project. Since these classes are relatively complex and the workload is relatively large, each class requires a staff member to write. For example, programmer A decides For the Dog class, programmer B writes the Cat class. These two classes have nothing to do with each other originally, but because the user needs them both to implement a method about "calling", this requires a constraint on them. Let them both inherit from The purpose of the IBark interface is to facilitate unified management. The other is to facilitate calling. Of course, the purpose can be achieved without using the interface. But in this case, this constraint is not so obvious. If such a class also has a Duck class, etc., compare Sometimes it is inevitable that someone will miss this method. Therefore, it is more reliable and more binding through the interface.

A simple explanation of the interface in C

# Suppose there are two types of programmers in our company: VB programmers refer to programmers who write programs in VB, represented by the class clsVBProgramer; Delphi programmers refer to programmers who write programs in Delphi, represented by the class clsVBProgramer clsDelphiProgramer class to represent. Every class has a WriteCode() method. The definition is as follows:

[csharp] view plain copy

class clsVBProgramer()  
{  
....  
WriteCode()  
{  
     //用VB语言写代码;  
}  
....  
}  
  
class clsDelphiProgramer()  
{  
....  
WriteCode()  
{  
    //用Delphi语言写代码;  
}  
   ....  
}

/*Now the company has a project and requires a programmer to write a program*/

class clsProject()  
{  
....  
WritePrograme(clsVBProgramer programer)//用VB写代码  
{  
    programer.WriteCode();  
}  
WritePrograme(clsDelphiProgramer programer)//重载方法,用Delphi写代码  
{  
    programer.WriteCode();  
}  
......  
}

In In the main program, we can write like this:

main()  
{  
   clsProject proj=new clsProject;  
   //如果需要用VB写代码  
   clsVBProgramer programer1=new clsVBProgramer;  
   proj.WritePrograme(programer1);  
   //如果需要用Delphi写代码  
   clsDelphiProgramer programer2=new clsDelphiProgramer;  
   proj.WritePrograme(programer2);  
}

But if the company comes to another C# programmer at this time, how can we change this program so that it can realize the function of writing programs in C#? We need to add a new class clsCSharpProgramer, and at the same time overload the WritePrograme (clsCSharpProgramer programer) method again in this clsProject class. This time it's more trouble. What if there are C programmers, C++ programmers, and Java programmers. Big trouble!

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

interface IProgramer()
{
WriteCode();
}

Then declare two classes and implement the IProgramer interface:

class clsVBProgramer():IProgramer{....WriteCode(){     //用VB语言写代码;}....}
class clsDelphiProgramer():IProgramer{....WriteCode(){    //用Delphi语言写代码;}   ....}

Make some modifications to the clsProject class:

class clsProject(){....WritePrograme(IProgramer programer){    programer.WriteCode();//写代码}......}
main(){   clsProject proj=new clsProject;   IProgramer programer;   //如果需要用VB写代码   programer=new clsVBProgramer;   proj.WritePrograme(programer);   //如果需要用Delphi写代码   programer=new clsDelphiProgramer;   proj.WritePrograme(programer);   }

If there are more programmers like C#, C, C++, and JAVA added, we only need to add them Add the relevant classes, and then make slight modifications in main() and it will be OK. The scalability is particularly good!

In addition, if we seal the clsProject class into a component, then when our users need to expand functions, we only need to make small external modifications to achieve it. It can be said that it is basically There is no need to change the components we have already sealed! Isn’t it very convenient and powerful!

The above is the detailed content of Detailed explanation of examples of C# interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn