C # explicit and implicit interface interfaces

Implementation of the interface is divided into: an implicit and explicit achieve achieved. If the class or structure to be achieved is a single interface, can be used to achieve implicit, or if the class inherits the structure of a plurality of interfaces that interfaces with the same name should member explicit implementation. Display implementation is to implement interface member by using the fully qualified name of the interface.
Use of explicit interface members of the body usually has two purposes:
1, as examples of explicit interface members of the body can not be accessed by a class, which can be implemented separately interface portion separated from the public interface. If only a class within this interface, the user class is not directly used to the interface, this interface member explicit execution body can play a role.
2, the executable explicit interface member avoids the confusion of the same name as the interface between the members. If a desired class of the same member names and types of interfaces return different implementations, which have an explicit interface to the members of the body to use. If there are no explicit interface member of the executive body, the name and return for different types of interface members, the class can not be achieved.
Example:
/// <Summary>
 /// IGoodbye interface
 /// </ Summary>
 public interface IGoodbye
 {
        void Speak ();
        void Bye ();
 }

/// <Summary>
 /// IHello interface
 /// < / Summary>
 public interface IHello
 {
   void Speak ();
 }

/// <Summary>
 /// ISay interface
 /// </summary>
 public interface ISay
 {
  void Say();
 }

---------------------------------
/// <summary>
 /// 隐式接口实现
 /// </summary>
 public class Hello : ISay, IHello
 {
  public Hello()
  {
  }

        public void Say()
        {
             Console.WriteLine("Say Hello");
        }

        public void Speak()
        {
             Console.WriteLine("Speak Hello");
        }
 }

/// <summary>
 /// 显式接口实现
 /// </summary>
 public class Speak : IHello, IGoodbye
 {
        public Speak()
        {
        }

        void IHello.Speak()
        {
            Console.WriteLine("Hello");
        }
           
        void IGoodbye.Speak()
        {
            Console.WriteLine("Good Bye");
        }

        void IGoodbye.Bye()
        {
             Console.WriteLine("Bye-Bye");
        }
 }


-------------------------
above explicitly realized, it can not be so called.
= New new Speak Speak Speak ();
speak.Speak (); this is unsuccessful.
Only so used: ((IHello) speak) .Speak ();

or this call:
Speak Speak = new new Speak ();
Speak .Speak (); // error: different methods
IHello Control = Speak;
control.Speak ( ); // call the Speak Speak method

in the code above the call to speak.Speak () it is wrong, because speak itself does not provide this method. control.Speak () call is the right way.
 Note: The interface itself does not provide an implementation of members of defined, it is merely a description of these members, which must rely on the class that implements the interface or support other interfaces.

--------------
implicitly achieve, you can directly call.
= New new hello Hello hello ();
hello.Say ();
hello.Speak ();

ON. Posted 2006-03-10 01:23 the On at The Way reading ( ... ) Comments ( ... ) edit collections

Reproduced in: https: //www.cnblogs.com/areliang/archive/2006/03/10/346801.html

Guess you like

Origin blog.csdn.net/weixin_34405557/article/details/93609987