什么是显式实现?在 C# 中,何时在接口中使用?

csharpserver side programmingprogramming更新于 2025/10/2 10:37:17

如果一个类实现了两个包含相同签名成员的接口,则在该类上实现该成员将导致两个接口都使用该成员作为其实现。

可以显式实现接口成员——创建一个仅通过接口调用且特定于该接口的类成员

示例

interface ICar{
   void display();
}
interface IBike{
   void display();
}
class ShowRoom : ICar, IBike{
   void ICar.display(){
      throw new NotImplementedException();
   }
   void IBike.display(){
      throw new NotImplementedException();
   }
}
class Program{
   static void Main(){
      Console.ReadKey();
   }
}

相关文章