MATLAB Object-Oriented Programming

(Joyce) #1

classdef SpecializedCopy < matlab.mixin.Copyable
properties
Prop1
Prop2 = datestr(now)
end
methods(Access = protected)
function cp = copyElement(obj)
cp = SpecializedCopy;
cp.Prop1 = obj.Prop1;
cp.Prop2 = datestr(now);
end
end
end


Create an object of the class and assign a value to Prop1:


a = SpecializedCopy;
a.Prop1 = 7


a =


SpecializedCopy with properties:


Prop1: 7
Prop2: '17-Feb-2015 17:51:23'


Use the inherited copy method to create a copy of a:


b = copy(a)


b =


SpecializedCopy with properties:


Prop1: 7
Prop2: '17-Feb-2015 17:51:58'


The copy (object b) has the same value for Prop1, but the subclass copyElement method
assigned a new value to Prop2. Notice the different timestamp.


Copy Properties That Contain Handles


Copying an object also copies the values of object properties. Object properties can
contain other objects, including handle objects. If you simply copy the value of a property


Implement Copy for Handle Classes
Free download pdf