Copying a handle variable results in another handle variable that refers to the same object. You can add copy functionality to your handle class by subclassingmatlab.mixin.Copyable
.The inheritedcopy
method enables you to make shallow copies of objects of the class. TheCopyObj
class shows the behavior of copy operations.
classdefCopyObj < matlab.mixin.CopyablepropertiesPropendend
Create an object of theCopyObj
类和分配处理of aline
object to the propertyProp
.
a = CopyObj; a.Prop = line;
Copy the object.
b = copy(a);
Confirm that the handle variablesa
andb
refer to different objects.
a == b
ans = logical 0
然而,line
object referred to bya.Prop
has not been copied. The handle contained ina.Prop
refers to the same object as the handle contained inb.Prop
.
a.Prop == b.Prop
ans = logical 1
For more detailed information on the behavior of the copy operation, seecopy
.
Customize handle object copy behavior by deriving your class frommatlab.mixin.Copyable
.Thematlab.mixin.Copyable
class is an abstract base class that derives from the handle class.matlab.mixin.Copyable
provides a template for customizing object copy operations by defining:
copy
— Sealed method that defines the interface for copying objects
copyElement
— Protected method that subclasses can override to customize object copy operations for the subclass
Thematlab.mixin.Copyable
copy
method, calls thecopyElement
method. Your subclass customizes the copy operation by defining its own version ofcopyElement
.
The default implementation ofcopyElement
makes shallow copies of all the nondependent properties.copyElement
copies each property value and assigns it to the new (copied) property. If a property value is a handle object,copyElement
copies the handle, but not the underlying data.
To implement different copy behavior for different properties, overridecopyElement
.For example, thecopyElement
method of theSpecializedCopy
class:
Creates a new class object
Copies the value ofProp1
to the new object
Reinitializes the default value ofProp2
by adding a timestamp when the copy is made
classdefSpecializedCopy < matlab.mixin.CopyablepropertiesProp1 Prop2 = datestr(now)endmethods(Access = protected)functioncp = copyElement(obj) cp = SpecializedCopy; cp.Prop1 = obj.Prop1; cp.Prop2 = datestr(now);endendend
Create an object of the class and assign a value toProp1
:
a = SpecializedCopy; a.Prop1 = 7
a = SpecializedCopy with properties: Prop1: 7 Prop2: '17-Feb-2015 17:51:23'
Use the inheritedcopy
method to create a copy ofa
:
b = copy(a)
b = SpecializedCopy with properties: Prop1: 7 Prop2: '17-Feb-2015 17:51:58'
The copy (objectb
) has the same value forProp1
, but the subclasscopyElement
method assigned a new value toProp2
.Notice the different timestamp.
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 that contains a handle object, you are actually copying the handle, not the object itself. Therefore, your copy references the same object as the original object. Classes that derive from thematlab.mixin.Copyable
class can customize the way the copy method copies objects of the class.
Suppose that you define a class that stores a handle in an object property. You want to be able to copy objects of the class and want each copy of an object to refer to a new handle object. Customize the class copy behavior using these steps:
Create a subclass ofmatlab.mixin.Copyable
.
OverridecopyElement
to control how the property containing the handle is copied.
Because the property value is a handle, create a new default object of the same class.
Copy property values from the original handle object to the new handle object.
TheHandleCopyclass customizes copy operations for the property that contains a handle object. TheColorPropclass defines the handle object to assign toProp2
:
Create an object and assign property values:
a = HandleCopy; a.Prop1 = 7; a.Prop2 = ColorProp;
Make a copy of the object using thecopy
method inherited frommatlab.mixin.Copyable
:
b = copy(a);
Demonstrate that the handle objects contained by objectsa
andb
are independent. Changing the value on objecta
does not affect objectb
:
a.Prop2.Color = 'red'; b.Prop2.Color
ans = blue
TheHandleCopy
class customizes the copy operation for objects of this class.
classdefHandleCopy < matlab.mixin.CopyablepropertiesProp1% Shallow copyProp2% Handle copyendmethods(Access = protected)functioncp = copyElement(obj)% Shallow copy objectcp = copyElement@matlab.mixin.Copyable(obj);% Get handle from Prop2hobj = obj.Prop2;% Create default objectnew_hobj = eval(class(hobj));% Add public property values from orig objectHandleCopy.propValues(new_hobj,hobj);% Assign the new object to propertycp.Prop2 = new_hobj;endendmethods(Static)functionpropValues(newObj,orgObj) pl = properties(orgObj);fork = 1:length(pl)ifisprop(newObj,pl{k}) newObj.(pl{k}) = orgObj.(pl{k});endendendendend
TheColorProp
class defines a color by assigning an RGB value to itsColor
property.
classdefColorProp < handlepropertiesColor ='blue';endend
Use theNonCopyable
property attribute to indicate that you do not want a copy operation to copy a particular property value. By default,NonCopyable
isfalse
, indicating that the property value is copyable. You can setNonCopyable
totrue
only on properties of handle classes.
For classes that derive frommatlab.mixin.Copyable
, the default implementation ofcopyElement
honors theNonCopyable
attribute. Therefore, if a property has itsNonCopyable
attribute set totrue
, thencopyElement
does not copy the value of that property. If you overridecopyElement
in your subclass, you can choose how to use theNonCopyable
attribute.
SetNonCopyable
totrue
in a property block:
properties(NonCopyable) Prop1end
If a property that is not copyable has a default value assigned in the class definition, the copy operation assigns the default value to the property. For example, theCopiedClass
assigns a default value toProp2
.
classdefCopiedClass < matlab.mixin.Copyableproperties(NonCopyable) Prop1 Prop2 = datestr(now)% Assign current timeendend
Create an object to copy and assign a value toProp1
:
a = CopiedClass; a.Prop1 = 7
a = CopiedClass with properties: Prop1: 7 Prop2: '17-Feb-2015 15:19:34'
Copya
tob
using thecopy
method inherited frommatlab.mixin.Copyable
:
b = copy(a)
b = CopiedClass with properties: Prop1: [] Prop2: '17-Feb-2015 15:19:34'
In the copyb
, the value ofProp1
is not copied. The value ofProp2
is set to its default value, which MATLAB®determined when first loading the class. The timestamp does not change.
Subclasses of thedynamicprops
class allow you to add properties to an object of the class. When a class derived fromdynamicprops
is also a subclass ofmatlab.mixin.Copyable
, the default implementation ofcopyElement
does not copy dynamic properties. The default value ofNonCopyable
istrue
for dynamic properties.
The default implementation ofcopyElement
honors the value of a dynamic propertyNonCopyable
attribute. If you want to allow copying of a dynamic property, set itsNonCopyable
attribute tofalse
.复制动态属性复制属性value and the values of the property attributes.
For example, this copy operation copies the dynamic property,DynoProp
, because itsNonCopyable
attribute is set tofalse
.The objectobj
must be an instance of a class that derives from bothdynamicprops
andmatlab.mixin.Copyable
:
obj =MyDynamicClass; p = addprop(obj,'DynoProp'); p.NonCopyable = false; obj2 = copy(obj);