23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

9 Classes and Objects<br />

a.currentValue = 0;<br />

a = class(a,'asset');<br />

case 1<br />

% if single argument of class asset, return it<br />

if (isa(varargin{1},'asset'))<br />

a = varargin{1};<br />

else<br />

error('Wrong argument type')<br />

end<br />

case 3<br />

% create object using specified values<br />

a.descriptor = varargin{1};<br />

a.date = date;<br />

a.type = varargin{2};<br />

a.currentValue = varargin{3};<br />

a = class(a,'asset');<br />

otherwise<br />

error('Wrong number of input arguments')<br />

end<br />

The function uses a switch statement to accommodate three possible<br />

scenarios:<br />

• Called with no arguments, the constructor returns a default asset object.<br />

• Called with one argument that is an asset object, the object is simply<br />

returned.<br />

• Called with three arguments (subclass descriptor, type, and current value),<br />

the constructor returns a new asset object.<br />

The asset constructor method is not intended to be called directly; it is called<br />

from the child constructors since its purpose is to provide storage for common<br />

data.<br />

The Asset get Method<br />

The asset class needs methods to access the data contained in asset objects.<br />

The following function implements a get method for the class. It uses<br />

capitalized property names rather than literal field names to provide an<br />

interface similar to other <strong>MATLAB</strong> objects.<br />

9-44

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

Saved successfully!

Ooh no, something went wrong!