[英]Call Derived Class Method if derived class exists
我有一个接口
interface ISampleInterface
{
void SampleMethod();
}
和一个继承自接口的类
public class ImplementationClass : ISampleInterface
{
// Explicit interface member implementation:
public virtual void SampleMethod()
{
Console.WriteLine("Base");
}
}
public class derived : ImplementationClass
{
public override void SampleMethod()
{
Console.WriteLine("child");
}
}
现在我想从派生类调用SampleMethod
如果用户创建派生类如果没有然后从作为基类的ImplementationClass
调用,基本上我必须在运行时决定天气派生类是否存在与SampleMethod
实现如果是,则调用否则转到基类SampleMethod
。
我想从派生类调用 SampleMethod 但派生类是由其他一些模块创建的,我必须检查派生类是否存在或不存在调用派生方法,否则调用基数学
根据对您的问题的评论,此代码已经按照您的描述工作 - 由于多态性,创建的对象将调用它最相关的方法。
在您的情况下,您覆盖其基类的虚拟方法,以便将调用该方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.