MATLAB Object-Oriented Programming

(Joyce) #1

classdef Stocks < finance.Assets & Taxable
methods
function s = Stocks(asset_args,tax_args,...)
if nargin == 0
...
end
% Call asset and member class constructors
[email protected](asset_args)
s@Taxable(tax_args)
...
end
end
end


Subclass Constructor Implementation


To ensure that your class constructor supports the zero arguments syntax, assign default
values to input argument variables before calling the superclass constructor. You cannot
conditionalize a subclass call to the superclass constructor. Locate calls to superclass
constructors outside any conditional code blocks.


For example, the Stocks class constructor supports the no argument case with the if
statement, but calls the superclass constructor outside of the if code block.


classdef Stocks < finance.Assets
properties
NumShares
Symbol
end
methods
function s = Stocks(description,numshares,symbol)
if nargin == 0
description = '';
numshares = 0;
symbol = '';
end
[email protected](description);
s.NumShares = numshares;
s.Symbol = symbol;
end
end
end


Design Subclass Constructors
Free download pdf