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.