0
abstract class SettingSaver
    {


        public abstract void add(string Name, string Value);
        public abstract void remove(string SettingName);


    }


class XMLSettings : SettingSaver
    {

        public override void add(string Name, string Value)
        {
            throw new NotImplementedException();
        }

        public override void remove(string SettingName)
        {
            throw new NotImplementedException();
        }




    }

无论如何,我可以将 XMLSettings 类中的 add 函数的名称更改为 addSetting 但确保它覆盖 SettingSaver 中的 add 函数?我知道它应该在派生类中被绝对覆盖,但只是想知道我是否可以使用不同的名称:) 提前谢谢 :D

4

4 回答 4

9

不,在 C# 中覆盖方法时不能更改名称。

当然,您可以重写该方法并通过调用不同的方法来实现它。

(在命名约定说明中,方法通常是 PascalCased,以大写字母开头。参数通常是 camelCased,以小写字母开头。)

于 2010-05-17T11:02:23.520 回答
3

不,你不能。

您可以添加另一个包含相同功能的方法,但不能隐藏或重命名继承的方法。

例如,框架中的 Stream 类有一个名为Close()的方法。这与调用 Dispose() 完全相同,但给定的名称更符合“打开”蒸汽以开始使用它的概念。他们没有删除 Dispose() 方法,只是为客户端提供了另一种(更好命名的)方法来访问相同的功能。

于 2010-05-17T11:03:00.803 回答
3

不 - 这会有点违反多态性(父母可以做的任何事情,孩子都应该能够做)。

您可以创建一个新方法并在旧方法上使用元数据来指导任何针对该类进行开发的人使用新类。

于 2010-05-17T11:03:16.670 回答
1

The method name and arguments is what identifies a method. If you change the method name, then you are identifying a different method. Since overriding is about defining the same method in a subclass, then you must use the same method name.

于 2010-05-17T11:07:04.083 回答