Can you create an interface for different class objects and then store objects of these different classes in an array

15 vues (au cours des 30 derniers jours)
hi,
I'm trying to create an array of class objects that all inherit from the same superclass but they vary in their implementation of inherited functions. I know i can create an array of class objects of the same class but i can't find a way to create an array of different class objects. In other languages this is where an interface would come in - I would declare an array of the interface, and then each individual object would be instantiated as a particular subclass - I hope this makes sense. THe folowing code is incorrect but I think what i'm looking would be somewhat like it:
% declare array of interfaceX arr(3) = interfaceX %instantiate each element as different class arr(1) = classA arr(2) = classB arr(3) = classC
Anybody got any ideas? I've looked at abstract classes, but unless i'm mistaken they simply serve as a template for inheriting classes.
Any help would be much appreciated - thanks!
Eoin

Réponses (3)

Walter Roberson
Walter Roberson le 22 Sep 2011
Which MATLAB version are you using? This was not possible until a fairly recent release; 2011a I think it was, but perhaps it was 2010b.
I seem to recall that people were generally not thrilled with the way it was implemented. It was something like that the type of the array was considered to be the type of the lowest superclass that all of the current array members could inherit from, so the array type could change as you added or removed members.
I haven't used this facility at all myself; I just browsed the discussion. Or perhaps it was another bad dream...
  2 commentaires
Walter Roberson
Walter Roberson le 22 Sep 2011
http://www.mathworks.com/help/techdoc/ref/brk7uzk.html#bsvqsj1
Heterogeneous Arrays
Tracking back through the releases, it appears to require R2011a .
Eoin
Eoin le 23 Sep 2011
I better upgrade then!
Thanks for your help Walter

Connectez-vous pour commenter.


Daniel Shub
Daniel Shub le 23 Sep 2011
Depending on what you want to do a cell array might work ....
arr = {classA};
arr = [arr;{ClassB}];
  2 commentaires
Eoin
Eoin le 23 Sep 2011
Good idea but afraid not - it doesn't like when i try to call a function from it.
Thanks for the help though
Daniel Shub
Daniel Shub le 23 Sep 2011
As I said, it depends on what you want to do. You might be able to use cellfun to call a function on it.

Connectez-vous pour commenter.


Eoin
Eoin le 22 Sep 2011
Hi Walter,
Thanks for your help
I'm on 2010b. DO you where i can find some example code for doing what you suggested. I've tried teh below to see if that does the trick
arr = classA;
arr = [arr;ClassB];
But it doesn't unfortunately.

Catégories

En savoir plus sur Construct and Work with Object Arrays dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by