Programming |
The Asset Constructor Method
The asset class is based on a structure array with four fields:
descriptor
-- 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)
type
-- The type of asset (e.g., savings, bond, stock)
currentValue
-- The current value of the asset (calculated from subclass data)
This information is common to asset child objects (stock, bond, and savings), so it is handled from the parent object to avoid having to define the same fields in each child class. This is particularly helpful as the number of child classes increases.
function a = asset(varargin) % ASSET Constructor function for asset object % a = asset(descriptor, type, currentValue) switch nargin case 0 % if no input arguments, create a default object a.descriptor = 'none'; a.date = date; a.type = 'none'; a.currentValue = 0; a = class(a,'asset'); case 1 % if single argument of class asset, return it if (isa(varargin{1},'asset')) a = varargin{1}; else error('Wrong argument type') end case 3 % create object using specified values a.descriptor = varargin{1}; a.date = date; a.type = varargin{2}; a.currentValue = varargin{3}; a = class(a,'asset'); otherwise error('Wrong number of input arguments') end
The function uses a switch
statement to accommodate three possible scenarios:
The asset constructor method is not intended to be called directly; it is called from the child constructors since its purpose is to provide storage for common data.
Example -- Assets and Asset Subclasses | The Asset get Method |
© 1994-2005 The MathWorks, Inc.