【问题标题】:How to inherit two or more interface with same method name and the derived class should be further inherited into another class in C# [closed]如何继承两个或多个具有相同方法名称的接口,派生类应进一步继承到C#中的另一个类[关闭]
【发布时间】:2021-10-01 07:19:42
【问题描述】:

我们如何在类中实现两个或多个具有相同方法名称的接口,并且派生类应该进一步继承到具有相同方法的新类中。

using System;  

interface A  
{  
    void Hello();  
}  

interface B  
{  
    void Hello();  
}  

class Test : A, B  
{  
    void A.Hello()  
    {  
        Console.WriteLine("Test Hello-A");  
    } 

    void B.Hello()  
    {  
        Console.WriteLine("Test Hello-B");  
    }  
}  

class Demo : Test {
   //what will be the code to override Hello method
}

public class MainClass  
{  
    public static void Main()  
    {  
        //How can we access the Hello method of Test & Demo class  
    }  
}

【问题讨论】:

  • 小修正:你不继承接口;你实现它们。
  • 问题是什么?你想覆盖什么代码?
  • @OlivierRogier 这不是在回答这个问题,因为这里有一个更复杂的案例,因为 Demo 类及其需求。但这绝对是有帮助的。

标签: c# inheritance interface overriding


【解决方案1】:
class Program
{
    static void Main()
    {
        IA objIa = new AB();
        objIa.Hello(10,20);
        IB objIb = new AB();
        objIb.Hello(10,20);
        ABC abc = new ABC();
        abc.Hello1(10,20);
        IA obj = new ABC();
        obj.Hello(10,20);
        IB obj1 = new ABC();
        obj1.Hello(10,20);
        Console.ReadLine();
    }
}

interface IA
{
    void Hello(int a, int b);
}
interface IB
{
    void Hello(int a, int b);
}

public class AB : IA, IB
{
    void IA.Hello(int a, int b)
    {
        Console.WriteLine("IA method");
    }

    void IB.Hello(int a, int b)
    {
        Console.WriteLine("IB method");
    }

    public virtual void Hello1(int a, int b)
    {
        Console.WriteLine("Hello1  method");
    }
}

public class ABC : AB,IA,IB
{
    void IA.Hello(int a, int b)
    {
        Console.WriteLine("ABC method");
    }

    void IB.Hello(int a, int b)
    {
        Console.WriteLine("ABC method");
    }
    public override void Hello1(int a, int b)
    {
        //base.add1(a, b);
        Console.WriteLine("Hello1 override method");
    }
}

【讨论】:

  • 请解释一下您的解决方案!
  • 如果两个接口(IA 和 IB)具有相同的方法,那么我们将在 AB 类下进行显式实现。如果我们想为 ABC 类继承 AB 类,那么它将只继承其他方法(Hello1),即在两个接口下都不可用。假设我们要使用接口方法,那么我们必须在类 ABC 中继承该接口。隐式或显式接口实现的方法不能在其他子类中覆盖。你能告诉我,解释清楚吗?我很乐意为您解释。
  • 我正在主持,虽然我不太喜欢这个话题。考虑到您的答案以及问题将保留多年,这有助于将来其他人对此事有更深入的了解。谢谢你的解释。
【解决方案2】:

在使用显式接口实现时存在一些限制,因为方法不能是virtual。但是您可以通过创建更多可以是虚拟的方法然后覆盖它们来解决此问题。

我对这段代码不太满意,但它完全回答了你的问题。

using System;

interface A
{
    void Hello();
}

interface B
{
    void Hello();
}

class Test : A, B
{
    public virtual void HelloA()
    {
        Console.WriteLine("Test Hello-A");
    }

    public virtual void HelloB()
    {
        Console.WriteLine("Test Hello-A");
    }

    void A.Hello()
    {
        this.HelloA();
    }

    void B.Hello()
    {
        this.HelloB();
    }
}

class Demo : Test
{
    //what will be the code to override Hello method
    public override void HelloA()
    {
    }

    public override void HelloB()
    {
    }
}

public class MainClass
{
    public static void Main()
    {
        //How can we access the Hello method of Test & Demo class
        Test test = new();
        test.HelloA();
        test.HelloB();

        Demo demo = new();
        demo.HelloA();
        demo.HelloB();

        // Another option will be to call it using the interfaces:
        List<A> itemsA = new();
        itemsA.Add(test);
        itemsA.Add(demo);

        foreach (A itemA in itemsA)
        {
            itemA.Hello();
        }

        List<B> itemsB = new();
        itemsB.Add(test);
        itemsB.Add(demo);

        foreach (B itemB in itemsB)
        {
            itemB.Hello();
        }
    }
}

【讨论】:

    【解决方案3】:

    如果您有如代码所示的显式私有实现,则需要再次在 Demo 中显式实现 AB

    class Demo : Test , A, B
    {
        void A.Hello()
        {
            Console.WriteLine("Demo Hello-A");
        }
    
        void B.Hello()
        {
            Console.WriteLine("Demo Hello-B");
        }
    }
    

    但是,据我所知,无论您如何转换,都无法从 Demo 的实例调用 Test 中的实现:

        var demo   = new Demo();
        var a_demo = (A)demo;
        var b_demo = (B)demo;
        var test   = (Test)demo;
        var a_test = (A)test;
        var b_test = (B)test;
    
        a_demo.Hello();
        b_demo.Hello();
        a_test.Hello(); //invokes impl in demo
        b_test.Hello(); //invokes impl in demo
    
        
    

    【讨论】:

      猜你喜欢
      • 2011-01-23
      • 1970-01-01
      • 2021-09-12
      • 2013-05-22
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多