I am using C#, but I think this is a pretty generic OO question. Suppose I have a class called Animal, and it has properties like LegCount, EyeCount, HasFur, EatsMeat, etc.
Let’s say I have an instance a of Animal. Suppose a has LegCount set to 4 and EyeCount set to 2.
Now, I’d like to create an instance d of type Dog, which inherits from Animal. I’d like to initialize d with all the values of a. I realize I could create a constructor or otherwise some other method that would take an Animal and spit out a new Dog with all the values copied in, but I was hoping there was some Object Oriented principle / trick that had me covered.
What I want to do, in plain English, is:
Create new Instance d of Dog, with all starting values from a. The key is “all”, as opposed to specifying each property individually.
When you design a class that inherits from some other class, you don’t need to list all the members it inherits. It just inherits all of them. So I am wondering if I can “inherit the values” on actual instances.
You can’t do what you’re asking for with some C# language construct, you have to manually write mapping or delegating code. Or, take a look at
AutoMapperfor that.