景德镇市建设厅网站,网站开发项目分析模板,网站内页没有排名,厦门优化公司在C#中#xff0c;子类并不直接“继承”父类继承的接口#xff0c;但子类的确会继承父类对接口的实现#xff08;如果父类实现了该接口#xff09;。这里有一些关键的概念需要澄清#xff1a;
接口继承#xff1a;当一个类实现了某个接口时#xff0c;它必须实现接口中…在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但这会破坏多态性 }}
}