MATLAB Object-Oriented Programming

(Joyce) #1
function obj = padAccID(obj)
ac = obj.AccountID;
acstr = num2str(ac);
if length(acstr) < 12
obj.AccountID = [acstr,repmat('0',1,12-length(acstr))];
end
end
end
methods (Static)
function obj = loadobj(a)
if isstruct(a)
obj = MyAccount;
obj.AccountID = a.AccountID;
obj = padAccID(obj);
elseif isa(a,'MyAccount')
obj = padAccID(a);
end
end
end
end

You do not need to implement a saveobj method. You are using loadobj only to ensure
that older saved objects are brought up to date while loading.

Maintaining Compatible Versions of a Class


The PhoneBookEntry class uses a combination of techniques to maintain compatibility
with new versions of the class.

Suppose that you define a class to represent an entry in a phone book. The
PhoneBookEntry class defines three properties: Name, Address, and PhoneNumber.

classdef PhoneBookEntry
properties
Name
Address
PhoneNumber
end
end

However, in future releases, the class adds more properties. To provide flexibility,
PhoneBookEntry saves property data in a struct using its saveobj method.

13 Saving and Loading Objects

Free download pdf