如何覆盖基类方法并添加参数 [英] How to override a Base Class method and add a parameter

查看:22
本文介绍了如何覆盖基类方法并添加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的基类中有这个:

protected abstract XmlDocument CreateRequestXML();

我尝试在我的派生类中覆盖它:

I try to override this in my derived class:

protected override XmlDocument CreateRequestXML(int somevalue)
{
    ...rest of code
}

我知道这是一个简单的问题,但是因为我引入了一个参数,所以问题是?问题是,某些派生类在其实现中可能有也可能没有参数.

I know this is an easy question but because I've introduced a param is that the issue? The thing is, some of the derived classes may or may not have params in its implementation.

推荐答案

当你重写一个方法时,除了用 override 这个词代替 virtual 或 abstract 之外,它必须与原始方法具有完全相同的签名.

When you override a method, with the exception of the word override in place of virtual or abstract, it must have the exact same signature as the original method.

您在这里所做的是创建一个新的不相关的方法.不可能引入参数并仍然覆盖.

What you've done here is create a new unrelated method. It is not possible to introduce a parameter and still override.

这篇关于如何覆盖基类方法并添加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆