MATLAB Object-Oriented Programming

(Joyce) #1
The Utility class is handle compatible. Therefore, you can use it in the derivation of
classes that are either handle classes or value classes. See “Class Introspection and
Metadata” for information on using meta-data classes.

Return Modified Objects

The resetDefaults method defined by the Utility class returns the object it modifies.
When you call resetDefaults with a value object, the method must return the modified
object. It is important to implement methods that work with both handle and value
objects in a handle compatible superclass. See “Object Modification” on page 5-63 for
more information on modifying handle and value objects.

Consider the behavior of a value class that subclasses the Utility class. The
PropertyDefaults class defines three properties, all of which have default values:

classdef PropertyDefaults < Utility
properties
p1 = datestr(rem(now,1)) % Current time
p2 = 'red' % Character vector
p3 = pi/2 % Result of division operation
end
end

Create a PropertyDefaults object. MATLAB evaluates the expressions assigned as
default property values when the class is first loaded. MATLAB uses these same default
values whenever you create an instance of this class in the current MATLAB session.

pd = PropertyDefaults
pd =

PropertyDefaults with properties:

p1: ' 4:42 PM'
p2: 'red'
p3: 1.5708

Assign new values that are different from the default values:

pd.p1 = datestr(rem(now,1));
pd.p2 = 'green';
pd.p3 = pi/4;

All pd object property values now contain values that are different from the default values
originally defined by the class:

12 How to Build on Other Classes

Free download pdf