Difference between new and override

后端 未结 14 1777
再見小時候
再見小時候 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 05:53

    virtual: indicates that a method may be overriden by an inheritor

    override: overrides the functionality of a virtual method in a base class, providing different functionality.

    new: hides the original method (which doesn't have to be virtual), providing different functionality. This should only be used where it is absolutely necessary.

    When you hide a method, you can still access the original method by up casting to the base class. This is useful in some scenarios, but dangerous.

提交回复
热议问题