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

在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(但这会破坏多态性)  
        }
    }
}
相关推荐
郝学胜-神的一滴28 分钟前
Effective Python 条款45:巧用属性注解平滑升级类属性
运维·开发语言·数据库·python·网络协议·numpy
不积跬步无以至千里mmbb2638 分钟前
【软件工艺师 rust相关课程】
开发语言·后端·rust
SEO_juper40 分钟前
HTTPS 混合内容检查工具:页面全站 https 了,但还有 http 资源在拖后腿
开发语言·网络协议·http·https·node.js·json·bash
神一样的老师2 小时前
WS63 BLE 配网实战:用 Python 在 PC 上替代手机完成 WiFi 配网
开发语言·python·智能手机
geovindu3 小时前
python: Decision Tree
开发语言·后端·python·决策树
张人玉3 小时前
C#如何使用FreeSql连接数据库
数据库·c#·freesql
时间的情敌4 小时前
Three.js 工业场景性能优化合集|大批量管线、InstancedMesh 实例化、合并 Geometry、显存释放
开发语言·javascript·性能优化·three.js
步峰代理-盾山4 小时前
R语言爬虫实战:使用httr与XML解析网页数据并进行统计分析
xml·开发语言·爬虫·r语言·网络爬虫
sharemylove14 小时前
Java集合框架全面详解
java·开发语言·经验分享·笔记·学习
郝学胜-神的一滴19 小时前
Effective Python 条款43:自定义容器类
开发语言·windows·python·pandas·scikit-learn·pyqt