Class Property: Array of Objects
Mostra commenti meno recenti
I would like to create a "Dependent Property" inside a "Main Class" that will handle a array of objects of "Sub Class".
Exemple:
classdef main_class
properties
number
end
properties (Dependent)
% prop will be a array of sub_class object
% the size of this array will depend/change on the "number" property
prop = sub_class();
end
end
If I set:
main = main_class();
main.number = 3;
I would like to get:
main.prop = [sub_class(1) sub_class(2) sub_class(3)]
To me is important that "prop" be a dependent property to automatic change the number of the elements in the array.
How could I do that? Thanks!
1 Commento
I would strongly recommend reconsidering your wish to have this as a dependent property. I use dependent properties a lot and I like them, but in this case they have some serious difficulties.
Creating objects in a dependent property is highly unlikely to be what you want to do. This means that every time you access the dependent property it will create a new array of objects, you will never be able to access the same objects within the class (though you can, of course, if you store main.prop in a local variable and use it thereafter) because they will keep getting replaced every time you get the property 'prop'.
Even if this does happen to produce the behaviour you want it is not very transparent and I would always do something as fundamental as object creation in a function named e.g. createXXXX to be clear what it is doing, not as a side-effect of accessing a dependent property.
Also it is not clear - is sub_class actually a sub class of main_class? As in does it inherit from it, or do you just call it sub_class because it sits inside it? Objects containing objects of their own subclass feels like an odd design to me that would cause a lot of potential problems. Maybe I'm just not thinking of a usage though...
Risposta accettata
Più risposte (2)
Andrew Newell
il 28 Apr 2017
You need to define a get method (see Access Methods for Dependent Properties). For example, the method block could look like this:
methods
function value = get.prop(obj)
value(obj.number) = sub_class(obj.number);
for ii=1:obj.number-1
value(ii) = sub_class(ii);
end
end
end
Note that, since sub_class would share this method, the dependent variable is endlessly recursive; if you ran
main = main_class();
main.number = 1;
then main.prop = main.prop.prop = ...
2 Commenti
Nycholas Maia
il 28 Apr 2017
Modificato: Nycholas Maia
il 28 Apr 2017
Andrew Newell
il 28 Apr 2017
If sub_class is actually a subclass of main_class, it inherits the properties and methods of main_class. That would include the property number and the method get.prop. So if you type
main.prop.prop
get.prop will create a sub_class object and return it as main.prop. Then, in main.prop, get.prop will create yet another sub_class object and return it as main.prop.prop. I agree with Adam and Guillaume - this is not the way to go!
Nycholas Maia
il 29 Apr 2017
0 voti
Categorie
Scopri di più su Graphics Objects in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!