Interface Content Overview 1 -- interface overview and interface attribute implementation

Source: Internet
Author: User

Preface:

Near the end of the year, I went to apply for a job with a friend. I even met a C # multiple-choice question and asked: what is not included in the interface? The answer is: method, attribute, event, and field. (I'm Sure You should selectFieldHowever, the exact cause cannot be detailed. These two days, I saw Steven John metsker's 《C # design mode"Interface Mode Design", Detailed descriptions of the interface, and interface content. Here, we will give an overview of it.

 

Interfaces and abstract classes:

To better understand interfaces, the most common problem is the difference between interfaces and abstract classes. As mentioned above, the blog article "Application of IL-essence of interfaces analyzed by Il" has been described in detail. Using the Il decompilation tool, we can conclude thatEssence of interfacesThat is, when compiled into an intermediate language, it is implemented using the implementation mechanism of abstract classes (implementing polymorphism !). However, interfaces and abstract classes are actuallyDifference,The details are as follows:

1. A class can implement any number of interfaces, but a maximum of one abstract class can be subclass;

2. An abstract class can include non-abstract methods, and all methods of an interface are abstract in effect;

3. An abstract class can declare and use variables, but an interface cannot;

4. the access modifier of methods in an abstract class can be: public, internal, protected, protected internal, or private. By default, all interfaces are public. When declaring interface members, access modifiers are not allowed;

5. An abstract class can define constructors, but an interface cannot;

 

The above five points differ mainly from each other in terms of usage.Use CasesIs different, that isUtilityCan be summarized as follows:Abstract classes are used in inheritance relationships with family-layer relationships, while interfaces are used in functional sense.

 

Implementation of attributes in the interface:

Generally, the interface is a corresponding method, but it canAttributePerform the same operation.Code.) First look at the following code:

// Description: sets up the iflyable interface and implements the bird class to demonstrate the implementation of the attribute in the interface.

//Copyright:Http://www.cnblogs.com/yangmingming

// Notes: For ease, you can set up interfaces and classes together with the verification code.

Namespace Contentofinterfacedemo
{
Public   Interface Iflyable
{
Int Distance { Get ;}
String Typeoffly { Get ; Set ;}
}
Public   Class BIRD: iflyable
{
Private   Int Distance;
Private   String Typeoffly;
Public   Int Distance
{
Get { Return Distance ;}
}
Public   String Typeoffly
{
Get { Return Typeoffly ;}
Set {Typeoffly = Value ;}
}
}
Class Program
{
Static   Void Main ( String [] ARGs)
{
Bird bird =   New Bird ();
// Bird. Distance = 10;
Bird. typeoffly =   " Using his wings. " ;
Console. writeline ( " Distance: {0}, typeoffly: {1} " , Bird. Distance, Bird. typeoffly );
}
}
}

After debugging, the result is as follows:

 

We can see that we have inherited the interface attributes!

 

Interface attribute implementation analysis:

The reason why attributes can be inherited like methods, fromEssentiallyIt is not difficult to understand. In this blog, I first learned about ildasm.exe -- il decompilation utility, and used the Il code to describe the implementation mechanism of attributes, that isConvert to the corresponding get and set functionsIn this way, the so-called attribute implementation in this article can be converted into the implementation of the method, that is, the problem is solved ~ (This shows the importance of analyzing the Il code !)

 

Appendix: Problem

An interface can contain methods, C # attributes, and indexers. An interface can also contain events, but cannot contain delegation. Why? Continue in the next section...

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.