Hierarchy Parent/Child from same class

3 views (last 30 days)
Sylvain
Sylvain on 11 Mar 2025
Answered: Steven Lord on 11 Mar 2025
I would like to add Parents and Childs between object of the same class.
Can I create properties 'Parent' and 'Child' with method setParent

Answers (1)

Steven Lord
Steven Lord on 11 Mar 2025
I would like to add Parents and Childs between object of the same class.
Do you mean you're writing a class and you want each instance to have properties Parent and Child (or Parents and Children?) that can contain another instance of the class? [How many parents and how many children would you need/want an instance of the class to support?]
Would you also need to allow Parent and/or Child to contain something other than an instance of the class, to reflect that an object has no parent and/or no children?
Can I create properties 'Parent' and 'Child' with method setParent
There's no function named setParent in MATLAB. [A few classes in MathWorks products have defined such a method, but I haven't checked to see what if anything those methods are doing in addition to setting a property.]
which -all setParent
'setParent' not found.
But you can have classes with properties that contain other objects. See for example the linked list class example in the documentation. Before you write such a class, you probably want to read this other documentation page as well.
You could define a method named setParent or a property set method named set.Parent if you wanted, but often you don't need to. You could use regular indexing operations to set or get the property value, as shown in the Property Access Syntax section on this documentation page.

Categories

Find more on Structures in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!