在C#中覆盖和新的关键字之间的区别是什么? [英] What is the difference between the override and new keywords in C#?

查看:210
本文介绍了在C#中覆盖和新的关键字之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

覆盖之间的区别是什么定义的类层次结构方法时,关键字在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

这篇关于在C#中覆盖和新的关键字之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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