MATLAB Object-Oriented Programming

(Joyce) #1

  • getCurrentValue — This method is the Stocks class implementation of the abstract
    method defined by the Assets class. It returns the current value of this asset.

  • get.SharePrice — The property get method for the dependent SharePrice
    property returns the current share price of this stock. For information on how to
    access web services from MATLAB, see the webread function.


Stocks Class Code

classdef Stocks < financial.Assets
properties
NumShares double = 0
Symbol string
end
properties (SetAccess = private)
Type = "Stocks"
end
properties (Dependent)
SharePrice double
end
methods
function sk = Stocks(description,numshares,symbol)
if nargin == 0
description = '';
numshares = 0;
symbol = '';
end
sk.Description = description;
sk.NumShares = numshares;
sk.Symbol = symbol;
end
function value = getCurrentValue(sk)
value = sk.NumShares*sk.SharePrice;
end
function pps = get.SharePrice(sk)
% Implement web access to obtain
% Current price per share
% Returning dummy value
pps = 1;
end
end
end

20 Designing Related Classes

Free download pdf