Fill object properties from another class object
Mostra commenti meno recenti
Hi there,
I load an object of a class 'xdTest' from an API and would like to add to this object one more property.
I first thought of creating a class 'Test' with my extra property inheriting the subclass 'xdTest' properties like :
classdef Test < xdTest
properties
myExtraProp
end
end
But the thing is that I can't manage to create a Test object and fill its inherited properties with an existing xdTest object.
Any idea of a way to fill or merge its inherited properties without having to do it for every property in a constructor method?
Thank you so much!
2 Commenti
A lot depends on what you intend to do with this object later on. As I'm sure you are aware there are two main ways to provide extended behaviour in this way. You can use inheritance as you are suggesting and the answers below pick up on or you can use aggregation/composition.
Which to use depends on your situation although I have heard the phrase (or similar) 'use aggregatation/composition when you can and inheritance when you have to'.
So in your case you could simply have your class Test include an object of the class xdTest rather than inherit from it. i.e. it would be one of its properties.
This comes with pros and cons, hence me saying it depends what further interaction you want as to which is better. Using inheritance you inherit all the methods and properties with their access specifiers set by the base class. Using aggregation/composition you can make the xdTest part of the class either private or public. If you make it private you can then provide only such access via your new class as is needed.
The whole point of all that was that if you use aggregation/composition you don't need to copy the properties of your xdTest object into a Test object because you just set the whole xdTest object as a property of the Test object.
Clément E.
il 30 Giu 2016
Modificato: Clément E.
il 30 Giu 2016
Risposta accettata
Più risposte (1)
Steven Lord
il 28 Giu 2016
0 voti
You want to call the superclass constructor to initialize the superclass properties, then have the subclass constructor set the properties defined only in the subclass? See this section from the documentation for instructions on how to call the superclass constructor from the subclass constructor.
6 Commenti
Guillaume
il 28 Giu 2016
Wouldn't the subclass need to have a constructor that accepts an object of the subclass (i.e. a copy constructor) for this to work?
Steven Lord
il 28 Giu 2016
No. See for example the four classes whose code is given at the end of this documentation page. Each of DocStock, DocBond, and DocSavings have constructors that accept different information specific to stocks, bonds, or savings accounts respectively. They use those asset-type-specific information to derive/compute the current value for that asset and pass that current value into the DocAsset base class constructor. Then they store the asset-type-specific information in the properties defined in each type's subclass.
Guillaume
il 28 Giu 2016
I don't see this as being the same at all. Using your Doc* example, what is being asked is given a DocAsset object and values for NumShare and SharePrice, create a DocStock object with these. So the DocStock constructor signature would be:
function this = DocStock(DocAssetobj, numShare, SharePrice)
this = ???? %create a copy of DocAssetobj
%I don't think there is a way other than copying each property individually
this.numShare = numShare;
this.SharePrice = SharePrice;
end
@Steven, @Guillame, you stucked to your guns, I believe both of you are right. What @Clément asked for was to convert from one class to the other and add some properties while keeping original class as superclass. I can see three options:
- Use @Guillame constructor solution and remember that each time we want to construct object of our class, we have to create original object first.
- Make constructor as @Steven suggested, and separately create conversion method like function classBObj = classA2classB(ClassAObj, extras)
- Combine those two behaviours in class constructor by checking class of first input and following dedicated scenario then.
While scenarios 2 and 3 may look more versatile, 1st one may be better in some scenarios. If the only construction case for creating classBObj is when classAObj already exist then 2nd scenario makes little sense, if it is not same rule applies to 1st scenario.
I don't see it as sticking to our gun, as I see as misunderstanding either on my part or on Steve's.
With your scenario 2 or 3, the question is still how do you get the properties of classAObj into the classBObj object. I don't think there's a mechanism other than copying the properties (which Clément wanted to avoid).
Moreover, the signature of classBObj = classA2classB(ClassAObj, extras) is that of a constructor (there's no classBObj input), so I don't see this being any different from option 1.
But then again, maybe I'm misunderstand something.
Jan Orwat
il 29 Giu 2016
Oh, sorry. It seems I misused the phrase. I meant you both are right. And yes, no matter which scenario we choose, the same problem to solve remains.
You are right, classA2classB should be a separate function or classA method then.
Categorie
Scopri di più su Class Introspection and Metadata in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!