Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //In Bicycle.cs file
- using System;
- namespace LearnInheritance
- {
- class Bicycle : Vehicle {
- public Bicycle(double Speed): base(Speed){
- Wheels =2;
- }
- //use override keyword to override parent/superclass methods
- public override void SpeedUp(){
- Speed +=5;
- if (Speed >15) {
- Speed = 15;
- }
- }
- public override void SlowDown(){
- Speed -=5;
- if (Speed<0){
- Speed =0;
- }
- }
- }
- }
- ============================================================================================================================
- //In Vehicle.cs file
- using System;
- namespace LearnInheritance
- {
- class Vehicle
- {
- public string LicensePlate
- { get; private set; }
- public double Speed
- { get; protected set; }
- public int Wheels
- { get; protected set; }
- public Vehicle(double speed)
- {
- Speed = speed;
- LicensePlate = Tools.GenerateLicensePlate();
- }
- //mark the methods with virtual keyword so as to allow child/subclass to override it
- public virtual void SpeedUp()
- {
- Speed += 5;
- }
- public virtual void SlowDown()
- {
- Speed -= 5;
- }
- public void Honk()
- {
- Console.WriteLine("HONK!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement