简体   繁体   English

为什么要在C#中显式覆盖虚方法?

[英]WHy should virtual methods be explicitly overridden in C#?

为什么要在C#中显式覆盖虚方法?

By declaring a method as virtual , you are stating your intention that the method can be overridden in a derived class. 通过将方法声明为virtual方法,您表明您的意图是可以在派生类中重写该方法。

By declaring your implementing method as override , your are stating your intention that you are overriding a virtual method. 通过将您的实现方法声明为override ,您表明您打算覆盖virtual方法。

By requiring that the override keyword be used to override a virtual method, the designers of the language encourage clarity, by requiring you to state your intentions. 通过要求使用override关键字来覆盖虚拟方法,语言的设计者通过要求您陈述您的意图来鼓励清晰。

If you don't add the override keyword, the method will be hidden (as if it had the new keyword), not overridden. 如果您不添加override关键字,则该方法将被隐藏(就像它具有new关键字一样),而不是被覆盖。

For example: 例如:

class Base {
    public virtual void T() { Console.WriteLine("Base"); }
}
class Derived : Base {
    public void T() { Console.WriteLine("Derived"); }
}

Base d = new Derived();
d.T();

This code prints Base . 此代码打印Base If you add override to the Derived implementation, the code will print Derived . 如果您向Derived实现添加override ,代码将打印Derived

You cannot do this in C++ with a virtual method. 您不能使用虚方法在C ++中执行此操作。 ( There is no way to hide a C++ virtual method without overriding it ) 没有覆盖它就无法隐藏C ++虚方法

It is because the C# team members are all skilled C++ programmers. 这是因为C#团队成员都是熟练的C ++程序员。 And know how incipient this particular bug is: 并且知道这个特定的bug是多么早期:

class Base {
protected:
    virtual void Mumble(int arg) {}
};

class Derived : public Base {
protected:
    // Override base class method
    void Mumble(long arg) {}
};

It is a lot more common then you might think. 这比你想象的要普遍得多。 The derived class is always declared in another source code file. 派生类始终在另一个源代码文件中声明。 You don't typically get this wrong right away, it happens when you refactor. 你通常不会马上弄错,它会在你重构时发生。 No peep from the compiler, the code runs pretty normal, just doesn't do what you expect it to do. 没有窥视编译器,代码运行正常,只是没有做你期望它做的事情。 You can look at it for an hour or a day and not see the bug. 您可以查看它一小时或一天而不会看到错误。

This can never happen in a C# program. 这在C#程序中永远不会发生。 Even managed C++ adopted this syntax, breaking with native C++ syntax intentionally. 即使是托管C ++也采用了这种语法,故意打破本机C ++语法。 Always a courageous choice. 永远是一个勇敢的选择。 IntelliSense takes the sting out the extra verbiage. IntelliSense消除了额外的措辞。

There's a lot of syntax tweaks in C# that resemble this kind of bug avoidance syntax. C#中有很多语法调整,类似于这种错误避免语法。


EDIT: and the rest of the C++ community agreed and adopted the override keyword into the new C++11 language specification. 编辑:其余的C ++社区同意并将override关键字引入新的C ++ 11语言规范。

Because it makes code more readable: 因为它使代码更具可读性:

class Derived : Base
{
    void Foo();
}

In C++, Foo may or may not be a virtual method, we can't tell by looking at the definition. 在C ++中,Foo可能是也可能不是虚方法,我们无法通过查看定义来判断。 In C# we know a method is virtual (or isn't) because there is either a virtual or override keyword. 在C#中,我们知道一个方法是虚拟的(或者不是),因为有一个virtual或override关键字。

Jason's comment below is a better answer. 杰森在下面的评论是一个更好的答案。

(edited for clarity) (为清晰起见编辑)

Not all virtual methods should be overridden, though all abstract methods should (and must) be. 并非所有虚拟方法都应该被覆盖,尽管所有抽象方法都应该(并且必须)。 As for why the 'override' keyword is explicit, that's because overriding and hiding behave differently. 至于为什么'override'关键字是显式的,那是因为覆盖和隐藏的行为有所不同。 A hiding method is not called through a reference to a base class, whereas an overridden method is. 隐藏方法不是通过对基类的引用来调用的,而重写的方法是。 This is why the compiler specifically warns about how you should use the 'new' keyword in the case where you are hiding rather than overriding. 这就是为什么编译器会特别警告你在隐藏而不是覆盖的情况下应该如何使用'new'关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM