Difference between new and override

后端 未结 14 1717
再見小時候
再見小時候 2020-11-22 05:36

Wondering what the difference is between the following:

Case 1: Base Class

public void DoIt();

Case 1: Inherited class

<         


        
14条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 06:03

    try following: (case1)

    ((BaseClass)(new InheritedClass())).DoIt()
    

    Edit: virtual+override are resolved at runtime (so override really overrides virtual methods), while new just create new method with the same name, and hides the old, it is resolved at compile time -> your compiler will call the method it 'sees'

提交回复
热议问题