MATLAB Object-Oriented Programming

(Joyce) #1
'Units ''%s'' is not supported.', newUnits);
end
end
end
end

Suppose that you create an instance of Odometer and set the following property values:

odObj = Odometer;
odObj.Units = 'km';
odObj.TotalDistance = 16;

When you save the object:


  • ConversionFactor is not saved because it is a Constant property.

  • TotalDistance is saved.

  • Units is not saved because it is a Dependent property.

  • PrivateUnits is saved and provides the storage for the current value of Units.


When you load the object:


  • ConversionFactor is obtained from the class definition.

  • TotalDistance is loaded.

  • Units is not loaded, so its set method is not called.

  • PrivateUnits is loaded from the saved object.


If the Units property was not Dependent, loading it calls its set method and causes the
TotalDistance property to be set again.

Property Value Computed from Other Properties


The Odometer2 class TripDistance property depends only on the values of two other
properties, TotalDistance and TripMarker.

The class avoids order dependence when initializing property values during the load
process by making the TripDistance property dependent. MATLAB does not save or
load a value for the TripDistance property, but does save and load values for the two
properties used to calculate TripDistance in its property get method.

classdef Odometer2
properties

13 Saving and Loading Objects

Free download pdf