C#中的方法隐藏和隐藏之间有什么区别? [英] What is the difference between method hiding and shadowing in C#?

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

问题描述

C#中的方法隐藏和隐藏之间有什么区别?它们是相同还是不同?我们可以称它们为多态(编译时或运行时)吗?

What is the difference between method hiding and shadowing in C#? Are they same or different? Can we call them as polymorphism (compile time or run time)?

推荐答案

C#中的方法隐藏和隐藏之间有什么区别?

What is the difference between method hiding and shadowing in C#?

阴影是另一个常用的隐藏术语. C#规范仅使用隐藏",但可以接受.

Shadowing is another commonly used term for hiding. The C# specification only uses "hiding" but either is acceptable.

您仅调用方法隐藏",但是除了方法隐藏以外,还有其他形式的隐藏.例如:

You call out just "method hiding" but there are forms of hiding other than method hiding. For example:

namespace N
{   
    class D {}
    class C 
    {
        class N
        {
            class D
            {
                 N.D nd; // Which N.D does this refer to?

嵌套类N在D中时会隐藏名称空间N.

the nested class N hides the namespace N when inside D.

我们可以称它们为多态(编译时还是运行时)吗?

Can we call them as polymorphism (compile time or run time)?

方法隐藏可以用于多态,是的.您甚至可以将方法隐藏与方法覆盖混合使用;通过隐藏旧的虚拟方法来引入新的虚拟方法是合法的;在那种情况下,选择哪种虚拟方法取决于接收器的编译时运行时类型.这样做非常令人困惑,如果可能,您应该避免这样做.

Method hiding can be used for polymorphism, yes. You can even mix method hiding with method overriding; it is legal to introduce a new virtual method by hiding an old virtual method; in that case which virtual method is chosen depends on the compile-time and run-time type of the receiver. Doing that is very confusing and you should avoid it if possible.

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

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