是否可以在同一个 class 中覆盖一个函数?

Is overriding of a function possible in the same class?

这是一个 objective 问题类型,但是是否可以在同一个 class 中覆盖一个函数?一般来说,准确地说是在 C# 中。如果可能,如何以及何时使用它?

刚刚测试过。 class 本身不会给编译器一个显示错误甚至警告的理由,但是当您调用该函数时,编译器会显示:

The call is ambiguous between the following methods or properties: 'method1' and 'method2'

从句法上讲,使用标准方法这是不可能的。但实际上你可以通过改变委托来做到这一点(更接近于自己的方法)。但这离通过对象、助手(通过接口联系人提供服务)来改变行为已经不远了——更加灵活。

class Test1
{
    public virtual void Test2() {

    }

    public override void Test2() {

    }
}

给你一个编译错误。所以,不,这是不可能的。

这来自 C# 语言规范 §10.6.4 Override methods

The method overridden by an override declaration is known as the overridden base method. For an override method M declared in a class C, the overridden base method is determined by examining each base class type of C, starting with the direct base class type of C and continuing with each successive direct base class type, until in a given base class type at least one accessible method is located which has the same signature as M after substitution of type arguments. For the purposes of locating the overridden base method, a method is considered accessible if it is public, if it is protected, if it is protected internal, or if it is internal and declared in the same program as C.

A compile-time error occurs unless all of the following are true for an override declaration:

  • An overridden base method can be located as described above.

  • There is exactly one such overridden base method. This restriction has effect only if the base class type is a constructed type where the substitution of type arguments makes the signature of two methods the same.

  • The overridden base method is a virtual, abstract, or override method. In other words, the overridden base method cannot be static or non-virtual.

  • The overridden base method is not a sealed method.

  • The override method and the overridden base method have the same return type.

  • The override declaration and the overridden base method have the same declared accessibility. In other words, an override declaration cannot change the accessibility of the virtual method. However, if the overridden base method is protected internal and it is declared in a different assembly than the assembly containing the override method then the override method’s declared accessibility must be protected.

  • The override declaration does not specify type-parameter-constraints-clauses. Instead the constraints are inherited from the overridden base method. Note that constraints that are type parameters in the overridden method may be replaced by type arguments in the inherited constraint. This can lead to constraints that are not legal when explicitly specified, such as value types or sealed types.

所以不,这是不可能的,覆盖修饰符用于扩展基础 class 方法,而不是用于隐藏当前 class.

中的方法定义

也许您的面试官在方法被覆盖后对方法选择感到困惑,例如 here

嗯,你不能。根据定义(来自 msdn):需要 override 修饰符来扩展或修改继承方法、属性、索引器或事件的抽象或虚拟实现。

您无法继承 class 本身 (see here),因此您无法在同一个 class.

中覆盖

一件事接近覆盖并且您可以做的是方法重载。

public class A
{
    void methodA()
    {
        //something here
    }

    void methodA(int i)
    {
        //something else here
    }
}

看起来无法覆盖。但是,我确实看到了它的用处。例如,如果想手动覆盖机器生成的代码。