代码之家  ›  专栏  ›  技术社区  ›  masoud ramezani

从具有相同方法名的多个接口继承

  •  61
  • masoud ramezani  · 技术社区  · 15 年前

    如果我们有一个从多个接口继承的类,并且这些接口有同名的方法,那么我们如何在我的类中实现这些方法呢?我们如何指定实现哪个接口的方法?

    8 回复  |  直到 6 年前
        1
  •  87
  •   Pete    11 年前

    通过显式实现接口,如下所示:

    public interface ITest {
        void Test();
    }
    public interface ITest2 {
        void Test();
    }
    public class Dual : ITest, ITest2
    {
        void ITest.Test() {
            Console.WriteLine("ITest.Test");
        }
        void ITest2.Test() {
            Console.WriteLine("ITest2.Test");
        }
    }
    

    使用显式接口实现时,函数在类上不是公共的。因此,为了访问这些函数,必须首先将对象强制转换为接口类型,或者将其赋给声明为接口类型的变量。

    var dual = new Dual();
    // Call the ITest.Test() function by first assigning to an explicitly typed variable
    ITest test = dual;
    test.Test();
    // Call the ITest2.Test() function by using a type cast.
    ((ITest2)dual).Test();
    
        2
  •  10
  •   Gopher    15 年前
        3
  •  8
  •   Mark Seemann    10 年前

    您可以实现这些接口中的一个或两个 explicitly .

    假设您有这些接口:

    public interface IFoo1
    {
        void DoStuff();
    }
    
    public interface IFoo2
    {
        void DoStuff();
    }
    

    您可以这样实现这两种功能:

    public class Foo : IFoo1, IFoo2
    {
        void IFoo1.DoStuff() { }
    
        void IFoo2.DoStuff() { }        
    }
    
        4
  •  3
  •   Sebastian    15 年前

    有时你甚至需要这样做:

    public class Foo : IFoo1, IFoo2
    {
        public void IFoo1.DoStuff() { }
    
        public void IFoo2.DoStuff()
        {
            ((IFoo1)this).DoStuff();
        }        
    }
    
        5
  •  3
  •   Bimal Das    8 年前

    你可以实现 一个显式接口 另一个很简单 .

    public interface ITest {
        void Test();
    }
    public interface ITest2 {
        void Test();
    }
    public class Dual : ITest, ITest2
    {
        public void Test() {
            Console.WriteLine("ITest.Test");
        }
        void ITest2.Test() {
            Console.WriteLine("ITest2.Test");
        }
    }
    

    ITest.Test 将是 默认实现。

    Dual dual = new Dual();
    dual.Test();
    ((ITest2)dual).Test();
    

    输出:

    Console.WriteLine("ITest.Test");
    Console.WriteLine("ITest2.Test");
    
        6
  •  2
  •   Rohit.P    13 年前
    public class ImplementingClass : AClass1, IClass1, IClass2
    
        {
            public override string Method()
            {
                return "AClass1";
            }
            string IClass1.Method()
            {
                return "IClass1";
            }
             string IClass2.Method()
            {
                return "IClass2";
            }
        }
    

    因此,当从不同的类调用时,必须将对象类型转换为所需的接口或抽象类。

    ImplementingClass implementingClass = new ImplementingClass();
    ((AClass1)implementingClass).Method();
    
        7
  •  2
  •   Ranajit kumar    10 年前
    public interface IDemo1
    {
     void Test();
    }
    public interface IDemo2
    {
     void Test();
    }
    public class clsDerived:IDemo1,IDemo2
    {
      void IDemo1.Test() 
      {
       Console.WriteLine("IDemo1 Test is fine");
      }
     void IDemo2.Test() 
      {
        Console.WriteLine("IDemo2 Test is fine");
      }
    }
    
    public void get_methodes()
    {
        IDemo1 obj1 = new clsDerived();
        IDemo2 obj2 = new clsDerived();
        obj1.Test();//Methode of 1st Interface
        obj2.Test();//Methode of 2st Interface
    }
    
        8
  •  0
  •   Vinayak Savale    6 年前

    答案是” By using explicit Interface implementation

    举个例子:

    using System;
    
    interface A
    
    {
            void Hello();
    }
    
    interface B
    
    {
        void Hello();
    }
    
    
    class Test : A, B
    
    {
        void A.Hello()
    
        {
            Console.WriteLine("Hello to all-A");
        }
    
        void B.Hello()
        {
            Console.WriteLine("Hello to all-B");
    
        }
    
    }
    
    public class interfacetest
    
    {
        public static void Main()
    
        {
            A Obj1 = new Test();
    
            Obj1.Hello();
    
            B Obj2 = new Test();
    
            Obj2.Hello();
        }
    
    }
    

    输出: 大家好 大家好