在覆盖之间的区别是什么
和新
定义的类层次结构方法时,关键字在C#?
What is the difference between the override
and new
keywords in C# when defining methods in class hierarchies?
下面的页面非常漂亮总结你的问题。
The following page summarizes your question very nicely.
知道何时使用替代和新关键字
摘要
替换:当基类的方法,在派生类中被覆盖,在派生类中使用的版本,即使调用code不知道对象是派生的类的一个实例。
Override: When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't "know" that the object was an instance of the derived class.
新:如果您使用替代的新的关键字,而不是在派生类中的方法不覆盖的方法在基类中,它只是隐藏它。
New: If you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it.
如果你不指定新的或替代,输出结果是一样的,如果指定新的你,但你还可以得到一个编译器警告(你可能不知道,你隐藏了一种方法,基类的方法,或者实际上你可能想要覆盖它,只是忘了,包括关键字)。
If you don't specify either new or overrides, the resulting output is the same as if you specified new, but you'll also get a compiler warning (as you may not be aware that you're hiding a method in the base class method, or indeed you may have wanted to override it, and merely forgot to include the keyword).
替换:虚拟/抽象/覆盖方法的类的基类使用
Override: used with virtual/abstract/override type of method in base class
新:当基类没有申报方法,虚拟/抽象/覆盖
New: when base class has not declared method as virtual/abstract/override