gpt4 book ai didi

c# - 从父类继承而无需重新实现父类的子类接口(interface)

转载 作者:太空宇宙 更新时间:2023-11-03 18:27:33 25 4
gpt4 key购买 nike

目前我有以下内容:

public class ChildClass : ParentClass
{...

ParentClass实现了一个接口(interface)如下(我需要ParentClass实例化所以不能抽象):

public class ParentClass : IParentClass
{...

我还希望子类实现一个接口(interface),以便我可以模拟这个类,但我希望 ParentClass 的继承成员对 ChildClass 接口(interface)可见。

因此,如果我在父类中有方法 MethodA(),我希望在使用 IChildClass 而不仅仅是 ChildClass 时能够调用此方法。

我能想到的唯一方法是覆盖 ChildClass 中的方法,在 IChildClass 中定义该方法,然后调用 base.MethodA() 但这似乎并不正确

最佳答案

如果我没理解错的话,你是说你想要在你的接口(interface)和你的类中有一个继承层次结构。

这就是你如何实现这样的事情:

public interface IBase 
{
// Defines members for the base implementations
}

public interface IDerived : IBase
{
// Implementors will be expected to fulfill the contract of
// IBase *and* whatever we define here
}

public class Base : IBase
{
// Implements IBase members
}

public class Derived : Base, IDerived
{
// Only has to implement the methods of IDerived,
// Base has already implement IBase
}

关于c# - 从父类继承而无需重新实现父类的子类接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30885950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com