Create an inherited class from a 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 get only "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? Actually I am trying to create a Decorator template where I want the class to add behavior to the base class.

+4
source share
7 answers

as an alternative:

 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){} } 

Thus, you can inherit any class from the car and fill in the contents of the car from the provided object. Inherited objects do not need to know anything about what needs to be installed. They simply pass the current Car object to the base class, and it does the job.

+6
source

Maybe I'll come a little later, but only if someone finds this useful:

You can use reflection. This requires a little more code than you suggested, but I think it still offers the brevity you are looking for.

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

I wrote this explicitly from your example to clearly illustrate the concept, but I think that it is best to use a general method that can be used in all such cases of your decision.

Hope this helps.

+4
source

Looking at your code, I'm not sure how it compiles. Your constructors are wrong because the base constructor does not know how to start a constructor that accepts a car type. It looks like you are trying to implement a decorator template, but you didn’t do it right. In fact, what you need is the ICar interface, which is implemented, and from Display() to SuperCar you should call car.Display() . You will also have to implement Make and Model on a Super car and make them get the car back. car.Model to correctly implement the decorator pattern.

 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"); } 
+1
source

Change private properties to protected properties, private properties are not accessible to anyone except the class that creates them, while protected variables can be accessed by inherited classes.

0
source

yep to make it work the way you need to set the base constructor, as shown below:

 public SuperCar(Car car):base(car.make,car.model) { this.car = car; } 
0
source

You do not quite implement the decorator template

You need an abstract base class to hold a 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."); } } 
0
source

Based on @JoshWheelock's answer, I wrote this method, which I injected into the shared file of my Xamarin project

It clones classes, so you can use the descendant to duplicate the base class, you can configure the T parameter

 //... #if WINDOWS_UWP using System.Reflection; #endif //... public void CloneIn<T>(T src, T dest) { #if WINDOWS_UWP var props = typeof(T).GetTypeInfo().DeclaredProperties.Where(p => !p.GetIndexParameters().Any()); #else var props = typeof(T).GetProperties().Where(p => !p.GetIndexParameters().Any()); #endif foreach (var prop in props) { if(prop.SetMethod!=null) prop.SetValue(dest, prop.GetValue(src)); } } 

While not tested in Android and iOS, regarding Android I have an emulator that carefreely stopped working properly from that week ...

0
source

Source: https://habr.com/ru/post/1310402/


All Articles