Understanding of interfaces in C #

Source: Internet
Author: User
Interface statement 1. An interface defines a contract. 2. interfaces can accommodate methods, C # attributes, events, and indexers. 3. In an interface declaration, we can declare zero or multiple members. 4. The default access type of all interface members is public. 5. If any modifier is included in the interface member declaration, a compiler error is generated. 6. Similar to a non-abstract class, an abstract class must provide the implementation of all the Members in the interface as long as these Members appear in the base class of the class.   Interface understanding 1. Interface-Oriented Programming Exploitation Oo A basic nature -- Polymorphism. The same method varies. Think about it like this, Client Compile yourselfProgramIf you directly write a program for a specific class, then this program has a breeze Client It will be affected, but if it is different for an interface, a specific class has changed, only the interface is known, and Client It can be completely unchanged. It is said that upper-level leaders are better at doing things, because what they can do is usually virtual for common people. The more virtual, the more difficult it is to make mistakes.
In this case Oo Is also applicable. 2. From another perspective, interface-Oriented Programming reflection Oo Another aspect -- Encapsulation. The interface encapsulates the specific implementation, and the Implementation can be switched without affecting the customer. 3. The function of an interface, in a word, is the type of class ). You can better manage different types of classes by assigning them to different interfaces. The essence of OO, I think, is the abstraction of objects, the interface that best reflects this point is. Why do we discuss that the design patterns are only for languages with abstract capabilities (such as C ++, Java, and C #) because the design patterns are studied, it is actually how to reasonably abstract. (Cowboy'sFamous sayingIs "abstract is the part of the image to be extracted". It seems to be ridiculous, but it is actually the most reasonable ).

 

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 ()
{
// Written in VBCode;
}
....
}

Class clsdelphiprogramer ()
{
....
Writecode ()
{
// Write the code in Delphi;
}
....
}

the company has a project that requires a programmer to write a program.
class clsproject ()
{< br> ....
writeprograme (clsvbprogramer programer) // write code with VB
{< br> programer. writecode ();
}< br> writeprograme (clsdelphiprogramer programer) // reload method, use Delphi to write code
{< br> programer. writecode ();
}< br> ......
}< br> in the main program, we can write as follows:
main ()
{< br> clsproject proj = new clsproject;
// if you need to use VB to write code
clsvbprogramer programer1 = new clsvbprogramer;
proj. writeprograme (programer1);
// if you need to write code using Delphi
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!

In addition, if we encapsulate the clsproject class into a component, when our users need to expand the function, we only need to make small modifications externally, it can be said that there is no need to modify the components we have already closed! Is it very convenient and powerful!

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.