Secure interface interface--Display implementation interface

Source: Internet
Author: User

Objective:
When we define the members of an interface, we do not need to write the access control, because it is the default public and can only be public. When a class is to implement this interface, it is natural to expose its members. I've been doing this for a long time.
Interface Interface1    {        string GetName (string name);    }

Class Class1:interface1    {public        string GetName (string name)        {            return name;    }}}


But there is nothing wrong with doing so. Until I learned the control development, I found that the original interface can be implemented in this way.
public class Dropdownlist:listcontrol, IPostBackDataHandler    {                protected virtual bool LoadPostData (string Postdatakey, NameValueCollection postcollection)        {            return false;        }        protected virtual void RaisePostDataChangedEvent ()        {        }        bool Ipostbackdatahandler.loadpostdata (string Postdatakey, NameValueCollection postcollection)        {            return this. LoadPostData (Postdatakey, postcollection);        }        void Ipostbackdatahandler.raisepostdatachangedevent ()        {this            . RaisePostDataChangedEvent ();        }    }


The LoadPostData and Raisepostdata methods are called by the page, but do not expose both methods. Calls can not be invoked with the instance of the class, can only be called by reference to the interface, so that the implementation of the interface can only be invoked by the interface reference.

From then on I thought The name of the secure interface (not sure if it is reasonable), in programming, I can also use this.
Class Class1:interface1    {        private string GetName (string name)        {            return name;        }        String Interface1.getname (string name)        {            name + = "interface";            return GetName (name);        }            }
Class program    {        static void Main (string[] args)        {            Class1 c = new Class1 ();            Interface1 i = (Interface1) C;            System.Console.WriteLine (I.getname ("Eric Wen"));        }    }

I think that this is more in line with Oo, the interface method is not universal, implementation and invocation are both clear.

Subsequent:
Read the reply and know that this is the display implementation interface (EXPLICIT interface member implementations).
Display Implementation Interface Description:
1. Explicit interface member execution cannot be accessed through full-name in method invocation, property access, and index indicator access. In fact, an explicit interface member execution body can only be accessed through an instance of an interface, referencing only the member name of the interface.
2. An explicit interface member executor cannot use any access limiter, nor can it be added abstract, virtual, override, or static modifiers.
3. Explicit interface member execution body and other members have different access methods. Because it cannot be accessed in the method invocation, property access, or index indicator access, the explicit interface member execution body is in some sense private. However, they can be accessed through an instance of the interface, and also have a certain public property.
4, only when the class is defined, the interface name is written in the base class list, and the discretionary, type, and return types defined in the class are fully consistent with the explicit interface member execution body, the explicit interface member execution body is valid
5, if a class in both display and implicit implementation of the interface, which is the display element can only be accessed through the interface reference, the implicit element can only be accessed directly through the class.

Why do you want to display the implementation interface?
1, because the explicit interface member execution body cannot access through the instance of the class, this can separate the implementation part of the interface from the public interface. If a class uses the interface only internally, and the consumer of the class does not directly use the interface, the explicit interface member execution body can play a role.
2. Explicit interface member execution body avoids confusion between interface members because of the same name. If a class wants to implement different implementations of the interface members with the same name and return type, this must be done using the explicit interface member execution body. If there is no explicit interface member execution, the class cannot be implemented for interface members with different names and return types.

This article reproduced: http://www.cnblogs.com/ericwen/archive/2007/12/28/1018181.html

Secure Interface interface--Display implementation interface

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.