欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

c# 子类继承父类接口问题

最编程 2024-09-30 07:28:40
...

在C#中,子类并不直接“继承”父类继承的接口,但子类的确会继承父类对接口的实现(如果父类实现了该接口)。这里有一些关键的概念需要澄清:

接口继承:当一个类实现了某个接口时,它必须实现接口中定义的所有方法。如果这个类有子类,那么子类并不直接“继承”接口(就像它不直接继承父类的字段或属性一样),但是,如果子类想要被当作该接口类型的实例来使用,它必须确保自己(或其任何父类)已经实现了接口中的所有方法。
接口方法的实现:如果父类实现了某个接口,那么子类就间接地拥有了接口方法的实现(如果子类没有覆盖这些方法的话)。然而,这并不意味着子类“继承”了接口,而是说子类通过继承父类而获得了对接口方法的实现。
多态性:在C#中,接口支持多态性。这意味着你可以将子类的实例赋给接口类型的变量,只要子类(或其任何父类)实现了该接口。这允许你在不知道具体实现类的情况下编写可重用的代码。

正常使用new

 interface IMyInterface
    {
        void DoSomething();
    }

    class ParentClass : IMyInterface
    {
        public void DoSomething()
        {
            Console.WriteLine("Doing something in ParentClass");
        }
    }

    class ChildClass : ParentClass
    {
        // ChildClass 并没有直接实现 IMyInterface,但它继承了 ParentClass 对 IMyInterface 的实现  

        // 如果需要,ChildClass 可以覆盖 DoSomething 方法来提供自己的实现  
        public new void DoSomething() // 注意:这是隐藏(new)而不是覆盖(override),因为接口方法不能被覆盖  
        {
            Console.WriteLine("Doing something in ChildClass");
        }

        // 或者,如果你想要真正地覆盖(但只能在接口方法的签名被某个虚方法或抽象方法显式实现时才能做到)  
        // 你需要在父类中有一个虚方法或抽象方法,然后在子类中覆盖它  
        // 但由于这里直接实现了接口,所以我们只能隐藏方法或使用其他设计模式  
    }

    class Program
    {
        static void Main(string[] args)
        {
            IMyInterface obj1 = new ParentClass();
            obj1.DoSomething(); // 输出:Doing something in ParentClass  

            IMyInterface obj2 = new ChildClass();
            obj2.DoSomething(); // 默认情况下,输出:Doing something in ParentClass(因为 ChildClass 隐藏了 DoSomething)  

            // 如果你想要 ChildClass 的 DoSomething 被调用,并且你正在处理接口类型的引用  
            // 你需要确保不隐藏父类的方法,或者通过其他方式(如类型转换)来调用 ChildClass 的方法  

            // 例如,通过显式类型转换调用 ChildClass 的新方法(注意这不是多态)  
            ((ChildClass)obj2).DoSomething(); // 输出:Doing something in ChildClass(但这会破坏多态性)  
        }
    }

在这里插入图片描述

修改后,让子类可在接口中被调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace testDebug
{
    interface IMyInterface
    {
        void DoSomething();
    }

    class ParentClass : IMyInterface
    {
        public virtual void DoSomething()
        {
            Console.WriteLine("Doing something in ParentClass");
        }
    }

    class ChildClass : ParentClass
    {
        // ChildClass 并没有直接实现 IMyInterface,但它继承了 ParentClass 对 IMyInterface 的实现  

        // 如果需要,ChildClass 可以覆盖 DoSomething 方法来提供自己的实现  
        public override void DoSomething() // 注意:这是隐藏(new)而不是覆盖(override),因为接口方法不能被覆盖  
        {
            Console.WriteLine("Doing something in ChildClass");
        }

        // 或者,如果你想要真正地覆盖(但只能在接口方法的签名被某个虚方法或抽象方法显式实现时才能做到)  
        // 你需要在父类中有一个虚方法或抽象方法,然后在子类中覆盖它  
        // 但由于这里直接实现了接口,所以我们只能隐藏方法或使用其他设计模式  
    }

    class Program
    {
        static void Main(string[] args)
        {
            IMyInterface obj1 = new ParentClass();
            obj1.DoSomething(); // 输出:Doing something in ParentClass  

            IMyInterface obj2 = new ChildClass();
            obj2.DoSomething(); // 默认情况下,输出:Doing something in ChildClass(因为 ChildClass override了 DoSomething)  

            // 如果你想要 ChildClass 的 DoSomething 被调用,并且你正在处理接口类型的引用  
            // 你需要确保不隐藏父类的方法,或者通过其他方式(如类型转换)来调用 ChildClass 的方法  

            // 例如,通过显式类型转换调用 ChildClass 的新方法(注意这不是多态)  
            ((ChildClass)obj2).DoSomething(); // 输出:Doing something in ChildClass(但这会破坏多态性)  
        }
    }
}

在这里插入图片描述

推荐阅读