Benefits of using an abstract classes vs. regular class

后端 未结 9 902
独厮守ぢ
独厮守ぢ 2021-01-31 17:52

I have decided to start doing small coding projects on my own that focus on code quality instead of code quantity and have a question about the use of abstract classes.

9条回答
  •  闹比i
    闹比i (楼主)
    2021-01-31 18:29

    This might help you, Lets consider traveler who may use any type of vehicle i.e car,cycle,bike etc...
    but all vehicles moves in the same way with different speed constraints so we can have one

    abstract class Avehicle    
    {  
            string fuel;  
            public void move()  
    {  
     sysout("moving");  
    }   
    } 
    

    but all vehicles breaking system is different

    interface Ivehicle    
    {  
    
            public void breakorstop();  
    }  
    class Traveler    
    {  
      Ivehicle v; 
    //Settrers and getters   
     public drive()  
    {  
    v.move();  
    }  
    public break()  
    {  
    v.breakorstop();  
    }  
    }
    

    So finally Car or Cycle or Bike classes can extend Avehicle and can Implement Vehicle interface

提交回复
热议问题