Difference between new and override

后端 未结 14 1739
再見小時候
再見小時候 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:10

    The functional difference will not be show in these tests:

    BaseClass bc = new BaseClass();
    
    bc.DoIt();
    
    DerivedClass dc = new DerivedClass();
    
    dc.ShowIt();
    

    In this exmample, the Doit that is called is the one you expect to be called.

    In order to see the difference you have to do this:

    BaseClass obj = new DerivedClass();
    
    obj.DoIt();
    

    You will see if you run that test that in the case 1 (as you defined it), the DoIt() in BaseClass is called, in case 2 (as you defined it), the DoIt() in DerivedClass is called.

提交回复
热议问题