MATLAB Object-Oriented Programming

(Joyce) #1

Maintain Class Compatibility


In this section...
“Rename Property” on page 13-23
“Update Property When Loading” on page 13-25
“Maintaining Compatible Versions of a Class” on page 13-26
“Version 2 of the PhoneBookEntry Class” on page 13-27

Rename Property


Suppose that you want to rename a property, but do not want to cause errors in existing
code that refer to the original property. For example, rename a public property called
OfficeNumber to Location. Here is the original class definition:

classdef EmployeeList
properties
Name
Email
OfficeNumber % Rename as Location
end
end

Use of a hidden dependent property can achieve the desired results.


  • In the class definition, set the OfficeNumber property attributes to Dependent and
    Hidden.

  • Create a property set method for OfficeNumber that sets the value of the Location
    property.

  • Create a property get method for OfficeNumber that returns the value of the
    Location location property.


While the OfficeNumber property is hidden, existing code can continue to access this
property. The Hidden attribute does not affect access.

Because OfficeNumber is dependent, there is no redundancy in storage required by
adding the new property. MATLAB does not store or save dependent properties.

Here is the updated class definition.

Maintain Class Compatibility
Free download pdf