C # understanding the functions of interfaces

Source: Internet
Author: User
C # understands the functions of interfaces in C # development. interfaces are very important and useful. However, many people do not know how to use interfaces and how to use them. Let's take a look at the functions of the interface and how to use it.
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 the code in Delphi;
}
} Now the company has a project that requires a programmer to write a program. Class clsproject ()
{
Writeprograme (clsvbprogramer programer) // write code with VB
{
Programer. writecode ();
}
Writeprograme (clsdelphiprogramer programer) // reload method, use Delphi to write code
{
Programer. writecode ();
}
}
// In the main program, we can write as follows:
Main ()
{
Clsproject proj = new clsproject;
// Use VB to write code
Clsvbprogramer programer1 = new clsvbprogramer;
Proj. writeprograme (programer1 );
// 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!

However, if you use an interface, it will be totally 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;
}
}

Class clsdelphiprogramer (): iprogramer
{
Writecode ()
{
// Write the code in Delphi;
}
}
// Modify the clsproject class as follows:
Class clsproject ()
{
Writeprograme (iprogramer programer)
{
Programer. writecode (); // write code
}
}

Main ()
{
Clsproject proj = new clsproject;
Iprogramer programer;
// Use VB to write code
Programer = new clsvbprogramer;
Proj. writeprograme (programer );
// 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! Add a CSHARP programmer as follows: // The clsproject class and iprogramer do not need to be changed. You only need to write a CSHARP programmer class.
Class clscsharpprogramer (): iprogramer // remember to implement the iprogramer Interface
{
Writecode ()
{
// Write code in CSHARP language;
}
}
// The usage is as follows:
Main ()
{
Clsproject proj = new clsproject;
Iprogramer programer;
// Use CSHARP to write code
Programer = new clscsharpprogramer;
Proj. writeprograme (programer );
}
In this way, if we encapsulate the clsproject class into a component, then when our users need to expand the function, we only need to make small modifications externally to implement it, it can be said that there is no need to modify the components we have already closed! Is it very convenient and powerful! Conversion from Yiyi garden blog Park blog Original article link http://www.cnblogs.com/lynnlin/archive/2008/06/25/1229928.html

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.