Fill object properties from another class object

조회 수: 20 (최근 30일)
Clément E.
Clément E. 2016년 6월 28일
댓글: Clément E. 2016년 7월 1일
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
Adam
Adam 2016년 6월 29일
편집: Adam 2016년 6월 29일
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.
Clément E. 2016년 6월 30일
편집: Clément E. 2016년 6월 30일
Thank you very much for having reviewed the different possibilities Adam. Composition is currently what I am using, but inheritance seemed to me more appropriate. You may have changed my mind :)

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2016년 6월 28일
Assuming that the base class does not have a copy constructor (a constructor that simply takes another object of the base class) I think the only way is indeed to copy all the properties. And that's assuming that the properties have public or protected set access.
You can automate that though:
classdef Test < xdTest
properties
myExtraProp
end
methods
function this = Test(xdTestobj, extraprop)
mco = ?xdTest;
for prop = mco.PropertyList'
if ismember(prop.SetAccess, {'public', 'protected'})
this.(prop.Name) = xdTestobj(prop.Name);
else
warning('could not copy property %s', prop.Name);
end
end
this.myExtraProp = extraprop;
end
end
end
  댓글 수: 7
Guillaume
Guillaume 2016년 6월 30일
Oh, yes you need to prevent the infinite recursion in the constructor!
The code I've written should skip over private setaccess properties. That's the whole point of the ismember filter. I must have missed something. As per my comment, you probably also need to check a few other properties such as readonly, transient, etc.
Unfortunately, you won't be able to copy the private properties.
Clément E.
Clément E. 2016년 7월 1일
Your code was working great, but I needed to copy private properties. I think I'll go with the Composition way which seems, after all, more appropriate for my case.
Thank you again for your precious help and time!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2016년 6월 28일
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
Guillaume
Guillaume 2016년 6월 29일
편집: Guillaume 2016년 6월 29일
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
Jan Orwat 2016년 6월 29일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by