MATLAB Object-Oriented Programming

(Joyce) #1
BankAccount Class Discussion
function obj = loadobj(s)
if isstruct(s)
accNum = s.AccountNumber;
initBal = s.AccountBalance;
obj = BankAccount(accNum,initBal);
else
obj.AccountListener = AccountManager.addAccount(s);
end
end

loadobj method:


  • If the load operation fails, create the
    object from a struct.

  • Recreates the listener using the
    newly created BankAccount object
    as the source.


For more information on saving and
loading objects, see “Save and Load
Process for Objects” on page 13-2
end
end
End of static methods block

End of classdef

Expand for Class Code


classdef BankAccount < handle
properties (Access = ?AccountManager)
AccountStatus = 'open'
end
properties (SetAccess = private)
AccountNumber
AccountBalance
end
properties (Transient)
AccountListener
end
events
InsufficientFunds
end
methods
function BA = BankAccount(accNum,initBal)
BA.AccountNumber = accNum;
BA.AccountBalance = initBal;
BA.AccountListener = AccountManager.addAccount(BA);
end
function deposit(BA,amt)
BA.AccountBalance = BA.AccountBalance + amt;
if BA.AccountBalance > 0
BA.AccountStatus = 'open';
end
end
function withdraw(BA,amt)
if (strcmp(BA.AccountStatus,'closed')&& BA.AccountBalance <= 0)
disp(['Account ',num2str(BA.AccountNumber),' has been closed.'])
return


Developing Classes — Typical Workflow
Free download pdf