What really is the purpose of “base” keyword in c#?

后端 未结 9 1187
误落风尘
误落风尘 2020-12-08 00:42

Thus for used base class for some commom reusable methods in every page of my application...

public class BaseClass:System.Web.UI.Page
{
   public string Get         


        
9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 00:48

    You will use base keyword when you override a functionality but still want the overridden functionality to occur also.

    example:

     public class Car
     {
         public virtual bool DetectHit() 
         { 
             detect if car bumped
             if bumped then activate airbag 
         }
     }
    
    
     public class SmartCar : Car
     {
         public override bool DetectHit()
         {
             bool isHit = base.DetectHit();
    
             if (isHit) { send sms and gps location to family and rescuer }
    
             // so the deriver of this smart car 
             // can still get the hit detection information
             return isHit; 
         }
     }
    
    
     public sealed class SafeCar : SmartCar
     {
         public override bool DetectHit()
         {
             bool isHit = base.DetectHit();
    
             if (isHit) { stop the engine }
    
             return isHit;
         }
     }
    

提交回复
热议问题