MATLAB Object-Oriented Programming

(Joyce) #1
AccountManager Class Discussion
function assignStatus(BA)
if BA.AccountBalance < 0
if BA.AccountBalance < -200
BA.AccountStatus = 'closed';
else
BA.AccountStatus = 'overdrawn';
end
end
end

The assignStatus method is the
callback for the InsufficentFunds
event listener. It determines the value of a
BankAccount object AccountStatus
property based on the value of the
AccountBalance property.

The BankAccount class constructor calls
the AccountManager addAccount
method to create and store this listener.
function lh = addAccount(BA)
lh = addlistener(BA, 'InsufficientFunds', ...
@(src, ~)AccountManager.assignStatus(src));
end

addAccount creates the listener for the
InsufficentFunds event that the
BankAccount class defines.

See “Control Listener Lifecycle” on page
11-28
end
end
end statements for methods and for
classdef.

Expand for Class Code

classdef AccountManager
methods (Static)
function assignStatus(BA)
if BA.AccountBalance < 0
if BA.AccountBalance < -200
BA.AccountStatus = 'closed';
else
BA.AccountStatus = 'overdrawn';
end
end
end
function lh = addAccount(BA)
lh = addlistener(BA, 'InsufficientFunds', ...
@(src, ~)AccountManager.assignStatus(src));
end
end
end

3 MATLAB Classes Overview

Free download pdf