MATLAB Object-Oriented Programming

(Joyce) #1

How to Define Handle-Compatible Classes


In this section...
“What Is Handle Compatibility?” on page 12-41
“Subclassing Handle-Compatible Classes” on page 12-43

What Is Handle Compatibility?


A class is handle compatible if:


  • It is a handle class

  • Its HandleCompatible attribute is set to true


The HandleCompatible class attribute identifies classes that you can combine with
handle classes when specifying a set of superclasses.

Handle compatibility provides greater flexibility when defining abstract superclasses. For
example, when using superclasses that support both handle and value subclasses, handle
compatibility removes the need to define both a handle version and a nonhandle version
of a class.

A Handle Compatible Class

The Utility class is useful to both handle and value subclasses. In this example, the
Utility class defines a method to reset property values to the default values defined in
the respective class definition:

classdef (HandleCompatible) Utility
methods
function obj = resetDefaults(obj)
mc = metaclass(obj);
mp = mc.PropertyList;
for k=1:length(mp)
if mp(k).HasDefault && ~strcmp(mp(k).SetAccess,'private')
obj.(mp(k).Name) = mp(k).DefaultValue;
end
end
end
end
end

How to Build on Other Classes

Free download pdf