Security interface-display implementation Interface

Source: Internet
Author: User
Preface:
When defining interface members, you do not need to write access controllers because they are public by default and can only be public. When a class needs to implement this interface, its members must be made public. I have been doing this for a long time.   Interface Interface1
{
StringGetname (StringName );
}

  Class Class1: interface1
{
Public   String Getname ( String Name)
{
ReturnName;
}
}
}

However, there is no error in doing so. When I learned how to develop controls, 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 page, but they are not disclosed. You cannot call an instance of a class when calling an API. You can only use an API reference to call an instance.

Now I thinkSecurity Interface This name (I do not know whether it is reasonable) can also be used in programming.   Class Class1: interface1
{
Private   String Getname ( String Name)
{
ReturnName;
}
String Interface1.getname ( String Name)
{
Name+ = "Interface";
ReturnGetname (name );
}

}

Class Program
{
Static   Void Main ( String [] ARGs)
{< br> class1 C = New class1 ();
interface1 I = (interface1) C;
system. console. writeline (I. getname ( " Eric Wen " );
}
}

I think this is more in line with OO. The interface methods are not commonly used, and the implementation and call are both clear.

Follow-up:
After reading the reply, you can see that this is the explicit interface (member implementations ).
Display implementation Interface Description:
1. The explicit interface member execution body cannot be accessed through full names in method call, attribute access, and index indicator access. In fact, the execution body of an explicit interface member can only be accessed through the interface instance and only by referencing the interface member name.
2. The execution body of an explicit interface member cannot use any access limiters, and abstract, virtual, override, or static modifiers cannot be added.
3. The explicit interface member execution body has different access methods than other members. Because it cannot be accessed by full name in method call, attribute access, and index indicator access, the explicit interface member execution body is private in a sense. However, they can also be accessed through the instance of the interface, but also have a certain public nature.
4. Only when the class is defined, the interface name is written in the base class list, when the full names, types, and return types defined in the class are exactly the same as the execution bodies of explicit interface members, the execution bodies of explicit interface members are valid.
5. If an interface is displayed and implicitly implemented in a class, the display element can only be directly accessed through interface reference, and the implicit element can only be directly accessed through the class.

Why display the implementation interface? purpose:
1. Because the execution body of the explicit interface member cannot be accessed through the instance of the class, the interface implementation part can be separately separated from the public interface. If a class only uses this interface internally, and the class user does not directly use this interface, this explicit interface member execution body can play a role.
2. Explicit interface member execution bodies avoid confusion between interface members due to the same name. If a class wants to adopt different implementation methods for interface members with the same name and return type, it must use the explicit interface member execution body. If there is no explicit interface member execution body, the class cannot be implemented for interface members of different names and return types.

Reference: http://www.chinaitpower.com/A/2004-12-09/136479.html

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.