I believe using the code below reproduces the unexpected behavior you write about, where a set on the top-level object does not propagate through all of the children:
>> t = test;
>> t.ref_To_Another_Instance_Of_This_Class = test;
>> t.a = 5;
>> t.ref_To_Another_Instance_Of_This_Class.a
ans =
5
>> t.ref_To_Another_Instance_Of_This_Class.ref_To_Another_Instance_Of_This_Class.a
ans =
3
This occurs because setting the child's "a" property in the set method of "a" bypasses the child's set method and just updates the property. I think this behavior makes sense with the understanding that the set function is defined on the class, and its requirement is that it not be re-entered.
In order to propagate the set all the way through the children, I would suggest the approach below, where "a" is made Dependent (you should get a Code Analyzer warning in your class advising this) and define a new method to perform the work of updating properties:
classdef test2 < handle
properties(Dependent)
a
end
properties
ref_To_Another_Instance_Of_This_Class
end
properties(Access=private)
pA;
end
methods
function this = test2( isSlave )
if nargin == 0
this.ref_To_Another_Instance_Of_This_Class = test2( true );
this.a = 3;
end
end
function set.a( this , newA )
disp( 'setter method called' )
this.updateA(newA);
end
function a = get.a(this)
a = this.pA;
end
function updateA(this, newA)
this.pA = newA;
if ~isempty(this.ref_To_Another_Instance_Of_This_Class)
this.ref_To_Another_Instance_Of_This_Class.updateA(newA);
end
end
end
end
This class can now be used to set "a" all the way through:
>> t2 = test2;
>> t2.ref_To_Another_Instance_Of_This_Class = test2;
>> t2.a = 5;
>> t2.ref_To_Another_Instance_Of_This_Class.a
ans =
5
>> t2.ref_To_Another_Instance_Of_This_Class.ref_To_Another_Instance_Of_This_Class.a
ans =
5
0 Comments
Sign in to comment.