Confusion about virtual/new/override

后端 未结 5 1442
暗喜
暗喜 2020-12-15 01:06

I am a bit confused about the virtual/new/override thing. Here\'s an example:

class A
{
    public virtual void mVVirt         


        
5条回答
  •  無奈伤痛
    2020-12-15 01:50

    Do you have warnings hidden? When I do what you've done, I get this warning:

    'ProjectName.ClassName.B.mVVirtual()' hides inherited member 'ProjectName.ClassName.A.mVVirtual()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

    If you used override in class B, you wouldn't have this problem; both cases would give you "C::mVVirtual". Since you're not using override in class B, there's an implicit new in front of the method. This breaks the inheritance chain. Your code is calling a method on type A, and there are no inheriting classes that override that method due to the implicit new. So it has to call class A's implementation.

提交回复
热议问题