Get property of the parent object

If object contain an other object from another class, how can I access to the proprieties of that object from the child object ? In other words related to the example I give, how can I use the variable saved in Parent in the child ?
My parent class:
classdef Parent < handle
properties
name = '' ;
child = {};
end
methods
function obj = Parent(name)
obj.name = name
end
function createChild(obj, name)
obj.child = child(name)
end
end
end
My child class:
classdef child < handel
properties
name = '';
end
methods
function obj = child(name)
obj.name = name;
end
function createBlock(obj)
add_block('library/body', strcat(parent.name, '/', obj.name)
end
end
end

7 Comments

Pierrick - under which circumstances would you want the child object to know about the parent and access its properties?
Hi Geoff, I want to use it to create dynamically a Simulink model named like "parent.name" in witch I can add a body with the name "child.name".
Everything work pretty well but I didn't find yet how I'm supposed to get the parent name in the function.
Pierrick, the problem is the child class, as you've written it has no relationship with the parent class, other than being created by it. It knows nothing about a parent. Hence, why Geoff asked you 'under which circumstances would you want the child object to know about the parent'.
As you have it you could just pass the parent in as an argument, but given that all you want is the parent's name you could just as well just pass in the parent name as an argument.
From a design perspective I favour Guillaume's answer below though and have used a similar setup many times. Whether you want only parent class objects to be able to create children or a 3rd party object/function to create a child from a parent is up to you, but that is just a design question really.
OK, maybe are parent and child not the right terms. In my program I have a body model (what I called parent) that is composed of several body segments (what I called child). So the body segments class does not inherit from the body model class. But maybe I better said that the body segment objects are encapsulated in the body model object. I apology if it not so clear but I am quite new with the object oriented programming and English is a foreign language for me.
That's no problem! English is my first (and only :( ) language but trying to name variables and classes and describe relationships between them in an easily understandable way is one of the biggest challenges I find in programming a system of many classes and variables!
Parent / Child or Body / Element is perfectly fine for a name and describe the relationship appropriately. What I've shown in my answer is a classic design pattern in OOP.
Calling it encapsulation in this case seems wrong to me.

Sign in to comment.

 Accepted Answer

Perhaps, this is what you want to do:
classdef Parent < handle
properties (SetAccess = private)
name = '';
child = [];
end
methods
function this = Parent(name)
this.name = name;
end
function createchild(this, name)
this.child = child(name, this) %The child is now informed of his parent
end
end
classdef Child < handle
properties (SetAccess = private)
name = '';
parent = [];
end
methods (Access = ?Parent) %Only Parent is allowed to construct a child
function this = Child(name, parent)
this.name = name;
this.parent = parent;
end
end
methods
function createblock(this)
add_block('library/body', sprintf('%s/%s, parent.name, this.name));
end
end
end

1 Comment

Great answer as far as logic... has a couple syntax errors though.
  • You're missing the end statment for your Parent classdef.
  • In the createchild function, the call to the Child constructor needs to be capitalized
Also, not an error, but would generate a warning... your end statements aren't all aligned with the corresponding keywords.

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!