Ste*_*ood 1 c# oop inheritance interface
目前我有以下内容:
public class ChildClass : ParentClass
{...
Run Code Online (Sandbox Code Playgroud)
ParentClass实现如下接口(我需要实例化ParentClass,因此不能是抽象的):
public class ParentClass : IParentClass
{...
Run Code Online (Sandbox Code Playgroud)
我也希望子类实现一个接口,以便我可以模拟这个类,但我希望ParentClass的继承成员对ChildClass接口可见.
因此,如果我在父类中有方法MethodA(),我希望能够在使用IChildClass而不仅仅是ChildClass时调用此方法.
我能想到的唯一方法是覆盖ChildClass中的方法,在IChildClass中定义该方法,并调用base.MethodA(),但这看起来并不正确
如果我理解正确,那么你说你想在接口和类中使用继承层次结构.
这就是你如何实现这样的事情:
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
}
Run Code Online (Sandbox Code Playgroud)