What I resorted to so far is this:
- supClass < handle creates two box1Class instances. Note that I initialize them in the constructor function and not the properties - this is because doing so will mean that additional instances of supClass will share the same instance of boxClass (see this answer).
- boxClass is a "generic" box that defines protected set methods, which can be overwritten in particular implementation of these set functions.
- box1Class and box2Class are these specific implementations. Notice that I link them to the supClass instance using a "parent" property, set when supClass is constructed.
- Then I can control the property values of each box1 and box2 using values from the parent or other properties in the tree.
I'm still wondering if there's a better way to do this? Consolidating all functionalities of box#Class implementations would be nice.
One thing I'm also not sure about is why this works correctly only when I use the < handle super-class. I understand it adds some functionality as a super-class that enables this, probably something with the set/get functions.
Here is a working example code - perhaps it would be useful to others:
classdef supClass < handle
properties
updateproperties = true
seed = pi
box1, box2
end
methods
function obj = supClass()
obj.box1 = box1Class(obj);
obj.box2 = box2Class(obj);
end
end
end
classdef boxClass
properties
x,y,z
end
methods
function obj = set.x(obj,value), obj.x = setx(obj,value); end
function obj = set.y(obj,value), obj.y = sety(obj,value); end
function obj = set.z(obj,value), obj.z = setz(obj,value); end
end
methods (Access = protected)
function value = setx(obj,value), end
function value = sety(obj,value), end
function value = setz(obj,value), end
end
end
classdef box1Class < boxClass
properties, parent, end
methods function obj = box1Class(parent), obj.parent = parent; end end
methods (Access = protected)
function value = setx(obj,value), if ~obj.parent.updateproperties, return; end
value = value * obj.parent.seed;
end
function value = sety(obj,value), if ~obj.parent.updateproperties, return; end
obj.parent.box2.y = value;
end
end
end
classdef box2Class < boxClass
properties, parent, end
methods function obj = box2Class(parent), obj.parent = parent; end end
end