MATLAB Object-Oriented Programming

(Joyce) #1
DefaultAsset Class Code

classdef DefaultAsset < financial.Assets
% file: +financial.@DefaultAsset/DefaultAsset.m
properties (SetAccess = private)
Type = "DefaultAsset"
end
methods
function obj = DefaultAsset
obj.Description = 'Place holder';
end
function value = getCurrentValue(~)
value = 0;
end
end
end

Operating on an Assets Array


The Assets class defines these methods to operate on heterogeneous arrays of asset
objects:


  • pie — Creates a pie chart showing the mix of asset types in the array.

  • makeReport — Uses the MATLAB table object to display a table of asset
    information.


To operate on a heterogeneous array, a method must be defined for the class of the
heterogeneous array and must be sealed. In this case, the class of heterogeneous arrays
is always the Assets class. MATLAB does not use the class of the individual elements of
the heterogeneous array when dispatching to methods.

makeReport Method Code

The Assets class makeReport method builds a table using the common properties and
getCurrentValue method for each object in the array.

function makeReport(obj)
numMembers = length(obj);
descs = cell(1,numMembers);
types(numMembers) = "";
values(numMembers) = 0;
for k = 1:numMembers
descs{k} = obj(k).Description;

20 Designing Related Classes

Free download pdf