Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //In Vehicle.cs file
- using System;
- namespace LearnInheritance
- {
- class Vehicle
- {
- public Vehicle (double speed){
- this.Speed = speed;
- LicensePlate = Tools.GenerateLicensePlate();
- }
- public string LicensePlate
- { get; private set; } //Since the LicensePlate and Speed properties defined in Vehicle are no longer accessed in Sedan or Truck, they no longer need to be protected. Switch those two setters to private.
- public double Speed
- { get; private set; }
- public int Wheels
- { get; protected set; }
- public void SpeedUp()
- {
- Speed += 5;
- }
- public void SlowDown()
- {
- Speed -= 5;
- }
- public void Honk()
- {
- Console.WriteLine("HONK!");
- }
- }
- }
- ============================================================================================================================
- //In Sedan.cs file
- using System;
- namespace LearnInheritance
- {
- class Sedan : Vehicle, IAutomobile
- {
- public Sedan(double speed) :base(speed) //inherit parent constructor with the same one argument - speed
- {
- //hence no need set values for Speed and LicensePlate here
- Wheels = 4;
- }
- }
- }
- ============================================================================================================================
- //In Truck.cs file
- using System;
- namespace LearnInheritance
- {
- class Truck : Vehicle, IAutomobile
- {
- public double Weight
- { get; }
- public Truck(double speed, double weight): base(speed) //also inherit parent constructor
- {
- //also no need set values for Speed and LicensePlate here
- Weight = weight;
- if (weight < 400)
- {
- Wheels = 8;
- }
- else
- {
- Wheels = 12;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement