MATLAB Object-Oriented Programming

(Joyce) #1

HandleCopy


The HandleCopy class customizes the copy operation for objects of this class.


classdef HandleCopy < matlab.mixin.Copyable
properties
Prop1 % Shallow copy
Prop2 % Handle copy
end
methods (Access = protected)
function cp = copyElement(obj)
% Shallow copy object
cp = [email protected](obj);
% Get handle from Prop2
hobj = obj.Prop2;
% Create default object
new_hobj = eval(class(hobj));
% Add public property values from orig object
HandleCopy.propValues(new_hobj,hobj);
% Assign the new object to property
cp.Prop2 = new_hobj;
end
end
methods (Static)
function propValues(newObj,orgObj)
pl = properties(orgObj);
for k = 1:length(pl)
if isprop(newObj,pl{k})
newObj.(pl{k}) = orgObj.(pl{k});
end
end
end
end
end


ColorProp


The ColorProp class defines a color by assigning an RGB value to its Color property.


classdef ColorProp < handle
properties
Color = 'blue';
end
end


Implement Copy for Handle Classes
Free download pdf