Invoke methods and access properties of a subclass by a super class

20 Ansichten (letzte 30 Tage)
Hi,
what is the safest way to do that in Matlab, if this is possible?!
I have an abstract relationship between 'smaller' objects and and object, which is composed out of those smaller objects. When I think about it, it must be a 'has-a' relationship. The 'bigger' object has many of the 'smaller' objects. All smaller objects, which the bigger object contains, contribute to build the bigger objects. There is a specific/certain/known number of smaller objects, which build up the bigger object literally.
I have started calculating with nested functions and I have developed (nearly almost) everything that is needed for the smaller objects, but nothing yet for the big object, which has/contains all the smaller objects.
I guess I should define a 'parent class', a 'super class' or a 'base classe' (all mean the same, right?!) for the bigger object and a 'sub class', a 'derived class' or a 'child class' (all mean the same, right?!) for the smaller objects?
I still haven't implemented any stuff for the bigger object yet, which has many smaller objects (inside of it) and I'm not sure how to start this task in a correct manner... Should I use a handle class for this type of inheritence? What do I urgently need to keep in mind, when confronted with such a situation? I've been reading through the matlab help pages and I think I've lost myself in reading?! ...
My 'thinking problem' at the moment is to grasp the idea, that a sub class inherits all methods from the a super class. Well, I haven't defined anything for the super class yet?! There is none for the sub class to inherit from the super class, because the super class just looks like this:
classdef MySuperClass
properties
%well ... It's just that this 'big object' has many of the other objects...
end
methods
%when I know how to think about this correctly, I would like to implement some method(s), which
%takes all the results from EVERY 'smaller object' and adds it up together to get a PROPERTY ...
%Of the super class! There we go! Right now I can think of three to four propertys which I can give
%for this class ...
end
end
My sub class has like 'tons of methods' all performed mainly on just one 'smaller object' at a time. And it has few properties.
What is the best way to communicate between these classes in a way that the super class can access the methods and the properties of the sub class and perform all necessary operations for one element at a time, BUT save the results in one of its properties? Does this sound logical? ...
I would like to implement this with the help of matlab classes and I appreciate your help, your advice and your encourgement!

Akzeptierte Antwort

Guillaume
Guillaume am 28 Apr. 2017
Bearbeitet: Guillaume am 29 Apr. 2017
Yes, parent class, base class, super class all mean the same. And from your description, it is not what you need. inheritance describes an 'is-a' relationship, not a 'has-a'. For a 'has-a', you want all these smaller objects to be member variables (properties) of the bigger class (container). Since there's no inheritance, the methods and properties of the smaller objects are completely different from the methods of the bigger object. That corresponds exactly to your description of the method/property of the container that sums up properties of the smaller object.
So your bigger class should be defined something like:
classdef bigclass
properties %possibly with (SetAccess = private)
smallobjects; %or smallclass smallobjects to indicate that smallobjects can only be of type smallclass
end
properties (Dependent)
area;
end
function this = bigclass() %constructor
%fill the smallobjects array however you want
this.smallobjects = smallclass.empty;
for n = 1:100
this.smallobjects(n) = bigclass(rand);
end
end
function area = get.area(this)
area = 0;
for n = 1:numel(this.smallobjects)
area = area + this.smallobjects(n).area;
end
end
end
If needed, the container class can be granted special access to private methods of the small class in order to preserve encapsulation (See Specify access to class members).
Note that this has nothing to do with handle versus value class. The choice of this is more to do with the purpose of the class rather than its hierarchy. The container class can either be handle or value. Your small class would probably be a value class.
  6 Kommentare
Guillaume
Guillaume am 29 Apr. 2017
"So you mean, it's better to have two independet classes and "define an object" of the smaller class as a property of the bigger class?"
Yes
" as long as I can invoke EVERYTHING from the big class and also have access from the bigger class to all properties of the smaller class?"
You can access any public function or method in the big class. The big class has access to all public members of the small class, and if needed you can also give it access to private properties (see link I posted previously). That latter option should be used sparingly if at all.
"Could I also define both separate classes as handle classes?"
As I said handle vs value is completely orthogonal to that design. It all depends on what copy behaviour you want. However, it's very rare to have a class with 100 objects as a handle class, so I would assume your small class should be a value class. The big one could be either. It does not change the design.
this.smallobjects = bigclass.empty;
That was a very unfortunate typo. This should have been:
this.smallobjects = smallclass.empty;
To initialise the array to an empty array of small objects.
Ahmed Hossam
Ahmed Hossam am 29 Apr. 2017
https://ch.mathworks.com/help/matlab/ref/empty.html#btr53n5-1
Ok, found it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Subclass Applications finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by