MATLAB Object-Oriented Programming

(Joyce) #1
Where obj is the output of the subclass constructor, SuperClass... is the name of a
superclass, and args are any arguments required by the respective superclass
constructor.

For example, the following segment of a class definition shows that a class called Stocks
that is a subclass of a class called Assets.

classdef Stocks < Assets
methods
function s = Stocks(asset_args,...)
if nargin == 0
% Assign values to asset_args
end
% Call asset constructor
s@Assets(asset_args);
...
end
end
end

“Subclass Constructors” on page 9-26 provides more information on creating subclass
constructor methods.

Reference Superclasses Contained in Packages

If a superclass is contained in a package, include the package name. For example, the
Assests class is in the finance package:

classdef Stocks < finance.Assets
methods
function s = Stocks(asset_args,...)
if nargin == 0
...
end
% Call asset constructor
[email protected](asset_args);
...
end
end
end

Initialize Objects Using Multiple Superclasses

To derive a class from multiple superclasses, initialize the subclass object with calls to
each superclass constructor:

12 How to Build on Other Classes

Free download pdf