23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

9 Classes and Objects<br />

if isa(name,'portfolio')<br />

p = name;<br />

else<br />

disp([inputname(1) ' is not a portfolio object'])<br />

return<br />

end<br />

otherwise<br />

% create object using specified arguments<br />

p.name = name;<br />

p.totalValue = 0;<br />

for k = 1:length(varargin)<br />

p.indAssets(k) = {varargin{k}};<br />

assetValue = get(p.indAssets{k},'CurrentValue');<br />

p.totalValue = p.totalValue + assetValue;<br />

end<br />

p.accountNumber = '';<br />

p = class(p,'portfolio');<br />

end<br />

Constructor Calling Syntax<br />

The portfolio constructor method can be called in one of three different ways:<br />

• No input arguments — If called with no arguments, it returns an object<br />

with empty fields.<br />

• Inputargumentisanobject—Iftheinputargumentisalreadyaportfolio<br />

object, <strong>MATLAB</strong> returns the input argument. The isa function checks<br />

for this case.<br />

• More than two input arguments — If there are more than two input<br />

arguments, the constructor assumes the first is the client’s name and the<br />

rest are asset subclass objects. A more thorough implementation would<br />

perform more careful input argument checking, for example, using the isa<br />

function to determine if the arguments are the correct class of objects.<br />

The Portfolio display Method<br />

The portfolio display method lists the contents of each contained object by<br />

calling the object’s display method. It then lists the client name and total<br />

asset value.<br />

9-60

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!