Can a child object reference to a parent object?
조회 수: 13 (최근 30일)
이전 댓글 표시
1 object (or instance) of a certain class can reference to another previously declared one if the the class had the "< handle" right at the beginning of its definition. If I defined a child class and declared a child object, can this object reference to a certain parent object? I mean if A is property in the parent class, can I by changing this property in the child class object (or instance) change it automatically in the parent class object (or instance) if they both reference to each other? I'm not sure whether the "class" function class(s,'class_name',parent); would had done the job, but it's now obsolete anyway. There must be a way. Thanks for helping.
댓글 수: 0
답변 (3개)
per isakson
2015년 1월 18일
편집: per isakson
2015년 1월 18일
Here is one way to let parent and child share a variable.
pc = ParentClass;
cc = ChildClass;
cc.A.a = 17;
pc.A.a
pc.A.a = 18;
cc.A.a
shows in the command window
ans =
17
ans =
18
where
classdef ParentClass < handle
properties
A = DataStore;
end
end
classdef ChildClass < ParentClass
end
classdef DataStore < handle
properties
a
end
end
 
However, I doubt ...
Guillaume
2015년 1월 19일
편집: Guillaume
2015년 1월 19일
Actually, I think it's possible to do what you want as long as the base class is a handle class.
First, a note of terminology, parent/child are not the terms traditionally used to describe inheritance. Matlab uses superclass/subclass and most everybody else uses base/derived.
If I've understood correctly, what you want to do is given an object of the base class (let's call it a), you want to create an object of the derived class (let's call it b) that is linked to a. Any change to the properties of a are reflected in b and any change to the properties of b derived from the base class are reflected in a.
I'm not sure it's a good idea, but it's doable in matlab, using dynamic properties. Note that the derived class doesn't actually derive from the base class:
classdef base < handle %base has to be a handle class
properties
someprop;
someotherprop;
end
end
classdef derived < dynamicprops & handle
%doesn't have to derive from handle if you don't want
%doesn't derive from base, inheritance implemented through dynamic props
properties
somemoreprop;
end
properties (Access = private)
baseref; %reference to base object
end
methods
function this = derived(baseobject)
validateattributes(baseobject, {'base'}, {'scalar'});
this.baseref = baseobject;
%now 'inherit' all the properties of the base object:
for propname = properties(baseobject)'
metaprop = addprop(this, propname{1});
metaprop.SetMethod = @(this, varargin) SetDispatch(this, propname{1}, varargin{:});
metaprop.GetMethod = @(this) GetDispatch(this, propname{1});
end
end
end
methods (Access = private)
function SetDispatch(this, propname, varargin)
%called whenever a dynamic property is set.
%just dispatch to the figure property
this.baseref.(propname) = varargin{:};
end
function varargout = GetDispatch(this, propname)
%called whenever a dependent dynamic property is read.
%just dispatch to the figure property
varargout{:} = this.baseref.(propname);
end
end
end
Usage example:
>> a = base; a.someprop = 45; a.someotherprop = [1 2 3]
a =
base with properties:
someprop: 45
someotherprop: [1 2 3]
>> b = derived(a); b.somemoreprop = {4 5 6}
b =
derived with properties:
somemoreprop: {[4] [5] [6]}
someotherprop: [1 2 3]
someprop: 45
>> a.someprop = -12; b
b =
derived with properties:
somemoreprop: {[4] [5] [6]}
someotherprop: [1 2 3]
someprop: -12
>>b.someotherprop = [0 0 0 0 0]; a
a =
base with properties:
someprop: -12
someotherprop: [0 0 0 0 0]
댓글 수: 2
Guillaume
2015년 1월 19일
That's what you get for me paraphrasing the command line instead of copy-pasting the output. I can assure you it all work as expected.
I'm amending the post to correct these typos.
참고 항목
카테고리
Help Center 및 File Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!