MATLAB Object-Oriented Programming

(Joyce) #1

Methods for Handle Compatible Classes


In this section...
“Methods for Handle and Value Objects” on page 12-46
“Modify Value Objects in Methods” on page 12-46

Methods for Handle and Value Objects


Objects passed to methods of handle compatible classes can be either handle or value
objects. There are two different behaviors to consider when implementing methods for a
class that operate on both handles and values:


  • If an input object is a handle object and the method alters the handle object, these
    changes are visible to all workspaces that contain the same handle.

  • If an input object is a value object, then changes to the object made inside the method
    affect only the value inside the method workspace.


Handle compatible methods generally do not alter input objects because the effects of
such changes are not the same for handle and nonhandle objects.

See “Object Modification” on page 5-63 for information about modifying handle and value
objects.

Modify Value Objects in Methods


If a method operates on both handle and value objects, the method must return the
modified object. For example, the setTime method returns the object it modifies:

classdef (HandleCompatible) Util
% Utility class that adds a time stamp
properties
TimeStamp
end
methods
function obj = setTime(obj)
obj.TimeStamp = now;
end
end
end

12 How to Build on Other Classes

Free download pdf