MATLAB Object-Oriented Programming

(Joyce) #1
classdef EmployeeList
properties
Name
Email
Location
end
properties (Dependent, Hidden)
OfficeNumber
end
methods
function obj = set.OfficeNumber(obj,val)
obj.Location = val;
end
function val = get.OfficeNumber(obj)
val = obj.Location;
end
end
end

Saving and Loading EmployeeList Objects

You can load old instances of the EmployeeList class in the presence of the new class
version. Code that refers to the OfficeNumber property continues to work.

Forward and Backward Compatibility

Suppose that you want to be able to load new EmployeeList objects into systems that
still have the old version of the EmployeeList class. To achieve compatibility with old
and new versions:


  • Define the OfficeNumber property as Hidden, but not Dependent.

  • Define the Location property as Dependent.


In this version of the EmployeeList class, the OfficeNumber property saves the value
used by the Location property. Loading an object assigns values of the three original
properties (Name, Email, and OfficeNumber), but does not assign a value to the new
Location property. The lack of the Location property in the old class definition is not a
problem.

classdef EmployeeList
properties
Name
Email

13 Saving and Loading Objects

Free download pdf