Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create inherited class from base class

public class Car 
{ 
    private string make;
    private string model;
    public Car(string make, string model)
    {
         this.make = make;
         this.model = model;
    }
    public virtual void Display()
    {
       Console.WriteLine("Make: {0}", make);
       Console.WriteLine("Model: {0}", model);
    }
    public string Make
    {
       get{return make;}
       set{make = value;}
    }
    public string Model
    {
       get{return model;}
       set{model = value;}
    }
}

public class SuperCar:Car
{
    private Car car;
    private int horsePower;
    public SuperCar(Car car)
    {
        this.car = car;
    }
    public int HorsePower
    {
       get{return horsePower;}
       set{horsepower = value;}
    }
    public override void Display()
    {
       base.Display();
       Console.WriteLine("I am a super car");
}

When I do something like

Car myCar = new Car("Porsche", "911");
SuperCar mySupcar = new SuperCar(myCar);
mySupcar.Display();

I only get "I am a supercar" but not the properties of my base class. Should I explicitly assign the properties of my base class in the SuperCar constructor? In fact I'm trying Decorator pattern where I want a class to add behaviour to a base class.

like image 382
Raj Avatar asked May 20 '10 16:05

Raj


4 Answers

alternatively:

public class Car
{
    public Car(string make, string model)
    {
         this.make = make;
         this.model = model;
    }


    public Car (Car car):this(car.Make, Car.Model){}
}

public class SuperCar : Car
{
  SuperCar(Car car): base(car){}
}

This way you can inherit any class from car, and have the car contents populate from the provided object. The inherited objects don't need to know anything about what to set. They just pass the current Car object onto the base class and it does the work.

like image 95
kemiller2002 Avatar answered Nov 03 '22 14:11

kemiller2002


I might be coming in a little late here, but just in the event that someone finds this useful:

You can use reflection. It requires a little more code than what you proposed, but I think it still offers the brevity you're searching for.

public SuperCar(Car car)
{
    var props = typeof(Car).GetProperties().Where(p => !p.GetIndexParameters().Any());
    foreach (var prop in props)
    {
        if (prop.CanWrite)
            prop.SetValue(this, prop.GetValue(car));
    }

    // Set SuperCarcentric properties
    // .
    // .
    // .
}

I wrote this explicitly from your example to clearly illustrate the concept, but I think this would be best made a generic method that can be used in all similar instances of your solution.

Hope this helps.

like image 22
Josh Wheelock Avatar answered Nov 03 '22 16:11

Josh Wheelock


Looking at your code I am not sure how it compiles. Your constructors are wrong because the base constructor won't know how to run a constructor that takes type car. It looks like you are trying to implement the decorator pattern but have not done it correctly. Really what you should have is an ICar interface that both implement and from Display() in SuperCar you should call car.Display() You'll also have to implement Make and Model on Super car and make them return car.Make & car.Model to implement the decorator pattern properly.

public interface ICar
{
    string Make {get; set;}
    string Model {get; set;}
    void Display();
}

public class Car :ICar
{ 
    private string make;
    private string model;
    public Car(string make, string model)
    {
         this.make = make;
         this.model = model;
    }
    public virtual void Display()
    {
       Console.WriteLine("Make: {0}", make);
       Console.WriteLine("Model: {0}", model);
    }
    public string Make
    {
       get{return make;}
       set{make = value;}
    }
    public string Model
    {
       get{return model;}
       set{model = value;}
    }
}

public class SuperCar:ICar
{
    private ICar car;
    private int horsePower;
    public SuperCar(ICar car)
    {
        this.car = car;
    }

    public string Make
    {
       get{return car.Make;}
       set{car.Make = value;}
    }
    public string Model
    {
       get{return car.Model;}
       set{car.Model = value;}
    }
    public int HorsePower
    {
       get{return horsePower;}
       set{horsepower = value;}
    }
    public override void Display()
    {
       car.Display();
       Console.WriteLine("I am a super car");
}
like image 1
Craig Suchanec Avatar answered Nov 03 '22 14:11

Craig Suchanec


You are not quite implementing the decorator pattern

You need an abstract base class to hold the decorated car

  public abstract class CarDecorator
  {
    protected Car DecoratedCar { get; private set; }

    protected CarDecorator(Car decoratedCar)
    {
      DecoratedCar = decoratedCar;
    }
  }

  public class SuperCar : CarDecorator
  {
    public SuperCar(Car car)
      : base(car)
    {
    }
    public int HorsePower
    {
      get { return horsePower; }
      set { horsepower = value; }
    }
    public override void Display()
    {
      DecoratedCar.Display()
      Console.WriteLine("Plus I'm a super car.");
    }
  }
like image 1
Daniel Avatar answered Nov 03 '22 15:11

Daniel