MATLAB Object-Oriented Programming

(Joyce) #1
However, the line object referred to by a.Prop has not been copied. The handle
contained in a.Prop refers to the same object as the handle contained in b.Prop.

a.Prop == b.Prop

ans =

logical

1

For more detailed information on the behavior of the copy operation, see
matlab.mixin.Copyable.copy.

Customize Copy Operation


Customize handle object copy behavior by deriving your class from
matlab.mixin.Copyable. The matlab.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:


  • matlab.mixin.Copyable.copy — Sealed method that defines the interface for
    copying objects

  • matlab.mixin.Copyable.copyElement — Protected method that subclasses can
    override to customize object copy operations for the subclass


The matlab.mixin.Copyable copy method, calls the copyElement method. Your
subclass customizes the copy operation by defining its own version of copyElement.

The default implementation of copyElement 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, override copyElement. For
example, the copyElement method of the SpecializedCopy class:


  • Creates a new class object

  • Copies the value of Prop1 to the new object

  • Reinitializes the default value of Prop2 by adding a timestamp when the copy is made


7 Value or Handle Class — Which to Use

Free download pdf