Main Content

Implement Copy for Handle Classes

Copy Method for Handle Classes

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 inheritedcopymethod enables you to make shallow copies of objects of the class. TheCopyObjclass shows the behavior of copy operations.

classdefCopyObj < matlab.mixin.CopyablepropertiesPropendend

Create an object of theCopyObj类和分配处理of alineobject to the propertyProp

a = CopyObj; a.Prop = line;

Copy the object.

b = copy(a);

Confirm that the handle variablesaandbrefer to different objects.

a == b
ans = logical 0

然而,lineobject referred to bya.Prophas not been copied. The handle contained ina.Proprefers 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 Copy Operation

Customize handle object copy behavior by deriving your class frommatlab.mixin.Copyable.Thematlab.mixin.Copyableclass is an abstract base class that derives from the handle class.matlab.mixin.Copyableprovides 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.Copyablecopymethod, calls thecopyElementmethod. Your subclass customizes the copy operation by defining its own version ofcopyElement

The default implementation ofcopyElementmakes shallow copies of all the nondependent properties.copyElementcopies each property value and assigns it to the new (copied) property. If a property value is a handle object,copyElementcopies the handle, but not the underlying data.

To implement different copy behavior for different properties, overridecopyElement.For example, thecopyElementmethod of theSpecializedCopyclass:

  • Creates a new class object

  • Copies the value ofProp1to the new object

  • Reinitializes the default value ofProp2by 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 inheritedcopymethod 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 subclasscopyElementmethod assigned a new value toProp2.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 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.Copyableclass can customize the way the copy method copies objects of the class.

Class to Support Handle Copying

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

  • OverridecopyElementto 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 thecopymethod inherited frommatlab.mixin.Copyable:

b = copy(a);

Demonstrate that the handle objects contained by objectsaandbare independent. Changing the value on objectadoes not affect objectb:

a.Prop2.Color = 'red'; b.Prop2.Color
ans = blue

HandleCopy

TheHandleCopyclass 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

ColorProp

TheColorPropclass defines a color by assigning an RGB value to itsColorproperty.

classdefColorProp < handlepropertiesColor ='blue';endend

Exclude Properties from Copy

Use theNonCopyableproperty attribute to indicate that you do not want a copy operation to copy a particular property value. By default,NonCopyableisfalse, indicating that the property value is copyable. You can setNonCopyabletotrueonly on properties of handle classes.

For classes that derive frommatlab.mixin.Copyable, the default implementation ofcopyElementhonors theNonCopyableattribute. Therefore, if a property has itsNonCopyableattribute set totrue, thencopyElementdoes not copy the value of that property. If you overridecopyElementin your subclass, you can choose how to use theNonCopyableattribute.

Set the Attribute to Not Copy

SetNonCopyabletotruein a property block:

properties(NonCopyable) Prop1end

Default Values

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, theCopiedClassassigns 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'

Copyatobusing thecopymethod inherited frommatlab.mixin.Copyable:

b = copy(a)
b = CopiedClass with properties: Prop1: [] Prop2: '17-Feb-2015 15:19:34'

In the copyb, the value ofProp1is not copied. The value ofProp2is set to its default value, which MATLAB®determined when first loading the class. The timestamp does not change.

Objects with Dynamic Properties

Subclasses of thedynamicpropsclass allow you to add properties to an object of the class. When a class derived fromdynamicpropsis also a subclass ofmatlab.mixin.Copyable, the default implementation ofcopyElementdoes not copy dynamic properties. The default value ofNonCopyableistruefor dynamic properties.

The default implementation ofcopyElementhonors the value of a dynamic propertyNonCopyableattribute. If you want to allow copying of a dynamic property, set itsNonCopyableattribute tofalse.复制动态属性复制属性value and the values of the property attributes.

For example, this copy operation copies the dynamic property,DynoProp, because itsNonCopyableattribute is set tofalse.The objectobjmust be an instance of a class that derives from bothdynamicpropsandmatlab.mixin.Copyable:

obj =MyDynamicClass; p = addprop(obj,'DynoProp'); p.NonCopyable = false; obj2 = copy(obj);

See Also

相关的话题