MATLAB Object-Oriented Programming

(Joyce) #1

methods
function s = saveobj(obj)
s.Name = obj.Name;
s.Address = obj.Address;
s.PhoneNumber = obj.PhoneNumber;
end
end


The loadobj method creates the PhoneBookEntry object, which is then loaded into the
workspace.


methods (Static)
function obj = loadobj(s)
if isstruct(s)
newObj = PhoneBookEntry;
newObj.Name = s.Name;
newObj.Address = s.Address;
newObj.PhoneNumber = s.PhoneNumber;
obj = newObj;
else
obj = s;
end
end
end


Version 2 of the PhoneBookEntry Class


In version 2 of the PhoneBookEntry class, you split the Address property into
StreetAddress, City, State, and ZipCode properties.


With these changes, you could not load a version 2 object in a previous release. However,
version 2 employs several techniques to enable compatibility:



  • Preserve the Address property (which is used in version 1) as a Dependent property
    with private SetAccess.

  • Define an Address property get method (get.Address) to build a char vector that is
    compatible with the version 2 Address property.

  • The saveobj method invokes the get.Address method to assign the object data to a
    struct that is compatible with previous versions. The struct continues to have only
    an Address field built from the data in the new StreetAddress, City, State, and
    ZipCode properties.


Maintain Class Compatibility
Free download pdf