MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

9 Classes and Objects case 'date' a.date = val; case 'currentValue' a.currentValue = val; otherwise error('Invalid field name') end end The subsasgn method enables you to assign values to the asset object data structure using two techniques. For example, suppose you have a child stock object s. (If you want to run this statement, you first need to create a stock constructor method.) s = stock('XYZ',100,25); Within stock class methods, you could change the descriptor field with either of the following statements s.asset(1) = 'ABC'; or s.asset.descriptor = 'ABC'; See the “The Stock subsasgn Method” on page 9-55 for an example of how the child subsasgn method calls the parent subsasgn method. The Asset display Method The asset display method is designed to be called from child-class display methods. Its purpose is to display the data it stores for the child object. The method simply formats the data for display in a way that is consistent with the formatting of the child’s display method. function display(a) % DISPLAY(a) Display an asset object stg = sprintf(... 'Descriptor: %s\nDate: %s\nType: %s\nCurrent Value:%9.2f',... a.descriptor,a.date,a.type,a.currentValue); disp(stg) 9-48

Example — Assets and Asset Subclasses The stock class display method can now call this method to display the data stored in the parent class. This approach isolates the stock display method from changes to the asset class. See “The Stock display Method” on page 9-57 for an example of how this method is called. TheAssetfieldcountMethod The asset fieldcount method returns the number of fields in the asset object data structure. fieldcount enables asset child methods to determine the number of fields in the asset object during execution, rather than requiring thechildmethodstohaveknowledgeoftheassetclass. Thisallowsyouto make changes to the number of fields in the asset class data structure without having to change child-class methods. function numFields = fieldcount(assetObj) % Determines the number of fields in an asset object % Used by asset child class methods numFields = length(fieldnames(assetObj)); The struct function converts an object to its equivalent data structure, enabling access to the structure’s contents. Designing the Stock Class A stock object is designed to represent one particular asset in a person’s investment portfolio. This object contains two properties of its own and inherits three properties from its parent asset object. Stock properties: • NumberShares — The number of shares for the particular stock object. • SharePrice — The value of each share. Asset properties: • Descriptor — The identifier of the particular asset (e.g., stock name, savings account number, etc.). • Date — The date the object was created (calculated by the date command). • CurrentValue — The current value of the asset. 9-49

9 Classes and Objects<br />

case 'date'<br />

a.date = val;<br />

case 'currentValue'<br />

a.currentValue = val;<br />

otherwise<br />

error('Invalid field name')<br />

end<br />

end<br />

The subsasgn method enables you to assign values to the asset object data<br />

structure using two techniques. For example, suppose you have a child stock<br />

object s. (If you want to run this statement, you first need to create a stock<br />

constructor method.)<br />

s = stock('XYZ',100,25);<br />

Within stock class methods, you could change the descriptor field with<br />

either of the following statements<br />

s.asset(1) = 'ABC';<br />

or<br />

s.asset.descriptor = 'ABC';<br />

See the “The Stock subsasgn Method” on page 9-55 for an example of how the<br />

child subsasgn method calls the parent subsasgn method.<br />

The Asset display Method<br />

The asset display method is designed to be called from child-class display<br />

methods. Its purpose is to display the data it stores for the child object. The<br />

method simply formats the data for display in a way that is consistent with<br />

the formatting of the child’s display method.<br />

function display(a)<br />

% DISPLAY(a) Display an asset object<br />

stg = sprintf(...<br />

'Descriptor: %s\nDate: %s\nType: %s\nCurrent Value:%9.2f',...<br />

a.descriptor,a.date,a.type,a.currentValue);<br />

disp(stg)<br />

9-48

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

Saved successfully!

Ooh no, something went wrong!