【问题标题】:My program has two interfaces and both have a method with the same name. So, how can we implement them in a child class inheriting both interfaces?我的程序有两个接口,并且都有一个同名的方法。那么,我们如何在继承这两个接口的子类中实现它们呢?
【发布时间】:2019-04-10 03:15:05
【问题描述】:

“我的程序有两个接口,并且都有一个同名的方法。那么,我们如何在继承这两个接口的子类中实现它们呢?另外,我们如何从子类中调用这两个方法?”

public interface A
{
    void Display();
}
public interface B
{
    void Display();
}

class Program: A, B
{
    void A.Display()
    {
        Console.WriteLine("Method of interface A");
    }
    void B.Display()
    {
        Console.WriteLine("Method of interface B");
    }
}

实现这两种接口方法是否正确?

【问题讨论】:

    标签: c#


    【解决方案1】:

    是的,您以正确的方式进行操作。

    在使用接口时,会出现这样一种情况,即一个类实现了两个接口,并且这两个接口都包含一个具有相同签名的成员。当类为接口成员提供定义时,它会混淆哪个成员获得定义,因为两者具有相同的名称。在这种情况下,我们将使用显式接口实现。

    请参阅下面的经典示例。

    using System;    
    
    namespace InterfaceDemo    
    {    
         //First Interface IDebitCard    
         interface IDebitCard    
         {     
           void CardNumber();    
         }    
    
         //Second Interface ICreditCard    
         interface ICreditCard    
         {    
           void CardNumber();    
         }    
    
         //Customer Class implements both the interfaces    
         class Customer: IDebitCard, ICreditCard    
         {   
    
           void IDebitCard.CardNumber()    
           {    
             Console.WriteLine("Debit Card Number: My Card Number is 12345XXXXX");    
           }    
    
           void ICreditCard.CardNumber()    
           {    
              Console.WriteLine("Credit Card Number: My Card Number is 98999XXXXX");    
           }    
    
           public void CardNumber()    
           {    
              Console.WriteLine("Customer ID Number: My ID Number is 54545XXXXX");    
           }    
         }    
    
         class Program    
         {    
           static void Main(string[] args)    
           {    
              Console.WriteLine("////////////////////- Implicit and Expliction Implementation -//////////////////// \n\n");    
              Customer customer = new Customer();    
              IDebitCard DebitCard = new Customer();    
              ICreditCard CreditCard = new Customer();    
    
              customer.CardNumber();    
              DebitCard.CardNumber();    
              CreditCard.CardNumber();    
    
              Console.ReadKey();    
           }    
         }    
    } 
    

    你可以在这里查看全文:Link

    【讨论】:

    • 非常感谢托尼汤姆的解释。它真的帮助我理解了它的实际工作原理。
    【解决方案2】:

    假设您想要 2 个不同的 逻辑 用于 2 个不同的 接口 实现,那么您将必须 显式 强制转换 显式 接口实现

     var program = new Program();
     ((A)program).Display();
     ((B)program).Display();
    

    输出

    Method of interface A
    Method of interface B
    

    真的有道理,编译器(或任何人)可能知道你想要哪个,调用者必须通过强制转换来选择

    Explicit Interface Implementation (C# Programming Guide)

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

    如果两个接口成员不执行相同的功能, 但是,这可能会导致其中一项或两项的错误实施 的接口。 可以实现接口成员 显式地——创建一个只通过 接口,并且特定于该接口。这是通过 用接口名称和句点命名类成员。

    【讨论】:

      【解决方案3】:

      你想达到什么目的?

      你可以这样做:

      public interface A
      {
        void Display();
      }
      
      public interface B : A
      {
        void SomeOtherMethod();
      }
      
      class Program: B
      {
        public void Display()
        {
          // implementation
        }
      
        public void SomeOtherMethod()
        {
          // implementation
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-04-09
        • 2020-12-09
        • 2012-03-30
        • 2022-11-18
        • 2019-08-04
        • 1970-01-01
        • 2013-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多