C # study notes 5,

Source: Internet
Author: User

C # study notes 5,

1.Explicit interface implementation: you must add the prefix of the interface name to the Member that implements the interface in the interface Implementation type. There is no need to add public or virtual modifiers, because explicit implementations can only be called through interfaces and cannot be called using implementation classes. To this end, the most typical practice is to transform an object into an interface.

2.Implicit Implementation of interfaces: implicit implementation is a normal reality. You do not need to add an interface name prefix to the implementation members, and the Implementation members are all public. Implicitly implemented members can make external calls in the implementation class or call through interfaces.

3.Comparison between display interface implementation and implicit interface implementation: the key difference between the implementation of implicit and explicit member interfaces is not the method declaration, but the accessibility from outside the class. When building a class hierarchy, it is necessary to model the "belonging" Relationship of the real world. For example, the giraffe "belongs to" mammals. These are "semantic" relationships, and interfaces are often used for modeling mechanisms. Explicit interface implementation is a technology that separates "mechanism considerations" from "Model considerations" and requires the caller to first convert an object to an interface (such as IComparable ), then we can think that the object is comparable. Generally, the best practice is to restrict the public layer of a class to a "full model" (that is, to implement it using an implicit Interface), with as few irrelevant mechanisms as possible.

4.There are three considerations for interface display implementation and implicit implementation.

① Whether a member is a core class function. If not, explicit implementation can be considered;

② Is the interface member name appropriate as a class member? If the purpose of a Member is not very clear in the implementation class, an explicit implementation should be considered;

③ Whether a class member with the same name already exists. If the class already has a method implementation, it can have the same name as an explicit interface member;

5.Interface inheritance: In an interface that implements inheritance, a fully qualified interface member name used for explicit interface member implementation must reference the region name of the interface that originally declares it. Whether multiple interfaces are provided or a composite interface is provided separately, this decision depends to a large extent on the requirements of the interface designer for the class theory.

6.Multi-inheritance through interfaces: although a class can only be derived from one base class, multiple interfaces can be implemented, so aggregation can be used to process multiple inheritance as in the previous chapter. You can view the code of Contact2 and Person2. the difference from the previous chapter is that the former is handled through classes, and the latter uses interfaces to specify signatures. IPerson ensures that the Person2 member and the member copied to Contact2 have the same signature, but this implementation is still not truly synonymous with "Multi-inheritance, because the new features added to Person2 are not added to Contact2 at the same time. If the implemented member is a method (not an attribute), there is a way to improve it. Specifically, it is) the "derived" additional function defines the interface extension method. (Of course, with the same signature, any instance method takes precedence over the extension method)

public class PdaItem{    public DateTime CreateTime { get; set; }    public DateTime ModifyTime { get; set; }    public string Name { get; set; }    protected string ObjectKey { get; set; } }public interface IPerson{    string FirstName { get; set; }    string LastName { get; set; }}public class Person2 : IPerson{    public string FirstName    {        get { throw new NotImplementedException(); }        set { throw new NotImplementedException(); }    }    public string LastName    {        get { throw new NotImplementedException(); }        set { throw new NotImplementedException(); }    }}public class Contact2 : PdaItem, IPerson{    private Person2 person2;    public string FirstName    {        get { return person2.FirstName; }        set { person2.FirstName = value; }    }    public string LastName    {        get { return person2.LastName; }        set { person2.LastName = value; }    }}

------------------- The above content is organized according to "C # the third edition of this topic ".

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.