In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The editor will share with you how to use the C# interface. I hope you will get something after reading this article. Let's discuss it together.
When the application problem of C# interface example is raised, suppose that there are two kinds of programmers in our company: VB programmers, which refers to programmers who write programs in VB, represented by the class clsVBProgramer; Delphi programmers refer to programmers who write programs in Delphi, which is represented by the class clsDelphiProgramer. Each class has a WriteCode () method.
The definition of C# API instance is as follows:
Class clsVBProgramer () {.... WriteCode () {/ / write code in VB language;}.... } class clsDelphiProgramer () {.... WriteCode () {/ / write code in Delphi;}.... }
Now the company has a project that requires a programmer to write a program.
C# interface instance
Class clsProject () {.... WritePrograme (clsVBProgramer programer) / / write code {programer.WriteCode ();} WritePrograme (clsDelphiProgramer programer) / / overloaded method with VB, code {programer.WriteCode ();} with Delphi. }
In the main program, we can write:
Example of C# API:
Main () {clsProject proj=new clsProject; / / if you need to write code clsVBProgramer programer1=new clsVBProgramer; proj.WritePrograme (programer1) in VB; / / if you need to write code clsDelphiProgramer programer2=new clsDelphiProgramer; proj.WritePrograme (programer2) in Delphi;}
But if there is another C # programmer in the company at this time, how can we change this program so that it can write programs in C #? We need to add a new class clsCSharpProgramer and overload the WritePrograme (clsCSharpProgramer programer) method again in this clsProject class. This is a lot of trouble. What if there are C programmers, C++ programmers and JAVA programmers? We're in big trouble!
But if you switch to an interface, it's completely different. Declare a programmer interface first:
Example of C# API:
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;}.... } make a modification to the class clsProject: class clsProject () {.... WritePrograme (IProgramer programer) {programer.WriteCode (); / / write code}. } main () {clsProject proj=new clsProject; IProgramer programer; / / if you need to write code programer=new clsVBProgramer; proj.WritePrograme (programer) in VB; / / if you need to write code programer=new clsDelphiProgramer; proj.WritePrograme (programer) in Delphi;}
If there are more programmers like Java, we just need to add their related classes, and then make a slight modification in main () to OK. The expansibility is very good!
In addition, if we seal the clsProject class into a component, then when our users need to expand the function, we only need to make small external changes to achieve, it can be said that there is no need to change we have sealed the component! Is not very convenient, very powerful!
After reading this article, I believe you have a certain understanding of "how to use the C# interface". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.