Hierarchy Parent/Child from same class

5 次查看(过去 30 天)
I would like to add Parents and Childs between object of the same class.
Can I create properties 'Parent' and 'Child' with method setParent

回答(1 个)

Steven Lord
Steven Lord 2025-3-11
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.

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by