C # Interface

Source: Internet
Author: User

First of all, I am a beginner in programming and an art-oriented pursuit of visual programming. I wrote this blog post in my spare time, but I am at a limited level, however, I still want to explain my understanding of the interface, hoping to provide some guidance to those who have not yet understood it. I wrote a blog post for the first time, I hope you can understand my shortcomings and correct them.

Interface concepts and declarations

1. An interface is a protocol used to define a program.

2. An interface can be a namespace or a member of a class and contain the signature of the following members: method, attribute, indexer, and event.

3. An interface can inherit one or more other interfaces. A class can inherit or implement multiple interfaces.

4. The non-abstract class that implements the interface must implement the members of its internal declaration.

5. Access modifiers cannot be displayed to interface members. The default access types of all interface members are public.

6. The class implementing the interface can explicitly implement the members of the interface. Explicitly implemented members cannot be accessed through class instances, but can only be accessed through interface instances.

Understanding of interfaces

1. all the Members in the interface are virtual. Why is it virtual? Because the interface only contains the declaration part of the members, and how to implement it is not the concern of the interface, it only exists as a specification.

2. The function of the interface is simply a classification of the Flag class. You can better manage different types of classes by assigning them to different interfaces. The essence of object-oriented is to further abstract the object, and the interface that best reflects this.

3. we often use the three-tier architecture when doing projects. If Class A is defined at the BLL layer and class B is defined at the DAL layer, instantiate an object of Class B in Class A and perform operations through this object. Obviously, if you want to change the data access method, you may need to modify the BLL-Layer Code, because an object of Class B is instantiated in Class. If an interface is used, the upper-layer class cannot directly rely on the lower-layer class but only one interface provided by the lower-layer class. This satisfies the design concept of "loose coupling.

Please allow me to write an example for further exploration

Animal eat (); dog: Animal eat () Console. writeLine (Console. writeLine (Main (dog mydog = dog (); mydog. eat (); mydog. drink ();}View Code

The intention of writing the small example above is to explain one point: the non-abstract class that implements the interface must implement all the members of this interface.

Single inheritance I think everyone should understand it easily, but I want to emphasize more inheritance. If the class implements two interfaces and these two interfaces contain members with the same signature, when this member is implemented in the class, both interfaces use this member as their implementation. For example:

Console. WriteLine (Main (}View Code

However, if two interface members perform different operations, this may result in incorrect implementation of one of the interfaces or incorrect implementation of both interfaces. This allows you to explicitly implement interface members. This is implemented by using the interface name and an English sentence to name the class members. For example:

Console. WriteLine (Console. WriteLine (Main (test mytest = Itest1 myItest1 = Itest2 myItest2 =}View Code

As defined in the Code above, the Itest1.fun class member can only be used through the Itest1 interface, while Itest2.fun () can only be used through itest2. These two methods are implemented separately, and neither of them can be directly used in the class.

Now, I know there are still a lot of questions about interfaces that I haven't mentioned yet, but my level is indeed quite the same. The first time I came out to show me ugly, I won't hurt ya .......

Related Article

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.