什么是C#中的界面强制转换?

use*_*261 6 c# casting interface

我确实理解如何编写一个接口在C#中工作,例如这里描述的: codeguru解释

interface IIntelligence
   {
      bool intelligent_behavior();
   }

   class Human: IIntelligence
   {
      public Human()
      {
          //.............
      }

/// Interface method definition in the class that implements it
      public bool intelligent_behavior()
      {
         Console.WriteLine("........");
         return true
      }
   }
Run Code Online (Sandbox Code Playgroud)

然而,我对以下接口转换过程感到困惑:

Human human = new Human();
// The human object is casted to the interface type
IIntelligence humanIQ = (IIntelligence)human;
humanIQ.intelligent_behavior();
Run Code Online (Sandbox Code Playgroud)

有一个类(在这种情况下是Human)实现一个接口,然后将它的实例人体反馈到接口的意义是什么?问题不在于它是如何运作的,而是为什么要这样做.

jga*_*fin 0

这是为了访问显式接口实现。

有时您想隐藏类实现接口的事实。这是通过显式实现接口来完成的。

public class MyClass : IInterface
{
     string IInterface.TheMethod(){}
}
Run Code Online (Sandbox Code Playgroud)